首页 技术 正文
技术 2022年11月8日
0 收藏 493 点赞 1,920 浏览 4745 个字

摘要: 微软动态CRM专家罗勇 ,回复304或者20190213可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。

备用键(Alternate Key)是个不错功能,可以保证该键值唯一,会在数据库中创建一个唯一索引,但是如果现有数据该键值就是有重复的呢?就会创建失败。

如果不解决就会带来误会,以为创建成功了,其实没有,还是会产生重复键值,这个在编号时候常用备用键来确保唯一。

一个个去查看是否创建成功太麻烦,我这里搞了个程序可以实现查看所有的备用键的创建状态,不多说了,上代码。

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;namespace RetrieveEntityAlternateKeys
{
class Program
{
static void Main(string[] args)
{
try
{
string inputKey;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["orgUrl"]));
AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials();
orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"];
orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"];
string needConfirm = ConfigurationManager.AppSettings["needConfirm"];
using (var orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials))
{
orgSvc.Timeout = new TimeSpan(, , );
WhoAmIRequest whoReq = new WhoAmIRequest();
var whoRsp = orgSvc.Execute(whoReq) as WhoAmIResponse;
var userEntity = orgSvc.Retrieve("systemuser", whoRsp.UserId, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname"));
Console.WriteLine(string.Format("欢迎【{0}】登陆到【{1}】", userEntity.GetAttributeValue<string>("fullname"), ConfigurationManager.AppSettings["orgUrl"]));
Console.WriteLine("本程序查看实体的备用键(Alternate Key)是否都成功创建了!");
if (needConfirm == "Y")
{
Console.WriteLine("当前处于需要确认才会继续的模式,若要继续请输入Y然后回车确认!");
inputKey = Console.ReadLine();
if (inputKey.ToUpper() == "Y")
{
RetrieveAlternateKeys(orgSvc);
}
else
{
Console.WriteLine("你选择了取消运行!");
}
}
else
{
RetrieveAlternateKeys(orgSvc);
}
}
Console.Write("程序运行完成,按任意键退出." + DateTime.Now.ToString());
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("程序运行出错:" + ex.Message + ex.StackTrace);
Console.ReadLine();
}
} private static void RetrieveAlternateKeys(OrganizationServiceProxy orgSvc)
{
const string functionName = "查看实体的备用键(Alternate Key)是否都成功创建了";
Console.WriteLine(string.Format("开始 {0} - {1}", functionName, DateTime.Now.ToString()));
try
{
RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest()
{
EntityFilters = EntityFilters.Entity,
RetrieveAsIfPublished = true
};
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)orgSvc.Execute(request);
foreach (EntityMetadata currentEntity in response.EntityMetadata)
{
RetrieveEntityRequest eReq = new RetrieveEntityRequest()
{
LogicalName = currentEntity.LogicalName,
EntityFilters = EntityFilters.All,
RetrieveAsIfPublished = true
};
var eRep = (RetrieveEntityResponse)orgSvc.Execute(eReq);
foreach (var item in eRep.EntityMetadata.Keys)
{
EntityKeyIndexStatus status = (EntityKeyIndexStatus)item.EntityKeyIndexStatus;
if (status != EntityKeyIndexStatus.Active)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format(@"{0} - {1} - {2} - {3}", item.EntityLogicalName, item.LogicalName, item.DisplayName.UserLocalizedLabel.Label, item.EntityKeyIndexStatus.ToString()));
}
else
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(string.Format(@"{0} - {1} - {2} - {3}", item.EntityLogicalName, item.LogicalName, item.DisplayName.UserLocalizedLabel.Label, item.EntityKeyIndexStatus.ToString()));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("运行 {0} 出现异常:{1}", functionName, ex.Message + ex.StackTrace));
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(string.Format("结束 {0} - {1}", functionName, DateTime.Now.ToString()));
Console.WriteLine("================================================");
} private static TProxy GetProxy<TService, TProxy>(
IServiceManagement<TService> serviceManagement,
AuthenticationCredentials authCredentials)
where TService : class
where TProxy : ServiceProxy<TService>
{
Type classType = typeof(TProxy); if (serviceManagement.AuthenticationType !=
AuthenticationProviderType.ActiveDirectory)
{
AuthenticationCredentials tokenCredentials =
serviceManagement.Authenticate(authCredentials);
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
.Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
}
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
.Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289