DuplicateKeyException:“无法添加具有已在使用的密钥的实体。” [英] DuplicateKeyException: “Cannot add an entity with a key that is already in use.”

查看:152
本文介绍了DuplicateKeyException:“无法添加具有已在使用的密钥的实体。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码片段支持 IndividualProfileProvider SqlProfileProvider - 这就是 string userName, SettingsPropertyValueCollection svc) 来自。在这次电话会议中,它非常直接。获取数据或将数据输入。在获取时,如果用户不存在,则svc保持原样(空)。在设置上,如果用户不存在,我创建一个新的 个人 row和 InsertOnSubmit() 。那是当前的运行时路径,因为dbo.Individual是空的。 System.Data.Linq.DuplicateKeyException:"无法添加一个已经在使用的密钥的实体。"出现在 dc.SubmitChanges(); 但是密钥不存在 - 表是空的(请假设连接正确)。

This snippet supports the IndividualProfileProvider : SqlProfileProvider – that’s where the (string userName, SettingsPropertyValueCollection svc) comes from.  It’s pretty straight forward within this call.  Get the data out or put the data in.  On get, if the user doesn’t exist, the svc is left as is (empty).  On set, if the user doesn’t exist, I create a new Individual row and InsertOnSubmit().   That’s the current runtime path as the dbo.Individual is empty.  System.Data.Linq.DuplicateKeyException: "Cannot add an entity with a key that is already in use." occurs at dc.SubmitChanges(); But the key does not exist - the table is empty (please assume that the connection is correct).

< font face = Calibri size = 3>有什么问题?根据我发现的其他帖子,我在 MyDataContext中的所有列上禁用了更新检查。 这没有解决它。

public static MyDataContext dc =

public static MyDataContext dc =

new MyDataContext ConfigurationManager .ConnectionStrings [" IndividualDB" ]。ConnectionString);

      new MyDataContext(ConfigurationManager.ConnectionStrings["IndividualDB"].ConnectionString);

static public void GetPropertyValuesFromDatabase( string userName, SettingsPropertyValueCollection svc)

static public void GetPropertyValuesFromDatabase(string userName, SettingsPropertyValueCollection svc)

< span style ="font-size:10pt; font-family:'Courier New'"> {

Guid userId = new Guid (userName);

      Guid userId = new Guid(userName);

< span style ="font-size:10pt; font-family:'Courier New'">

if (! string .IsNullOrEmpty(userName))

      if (!string.IsNullOrEmpty(userName))

{

个人玩家;

            Individual Player;

var 玩家= dc.Individuals.Where(p => p.UserId == userId);

            var Players = dc.Individuals.Where(p => p.UserId == userId);

如果(Players.Count()> 0)

            if (Players.Count() > 0)

{

Player = Players.First();

}

else

{

< span style ="color:blue"> return ;

                  return;

}

svc [" IsActive" ]。PropertyValue = Player.IsActive;

            svc["IsActive"].PropertyValue = Player.IsActive;

sv c [" Nickname" ]。PropertyValue = Player.Nickname;

            svc["Nickname"].PropertyValue = Player.Nickname;

svc [" EmailAddress" ]。 PropertyValue = Player.EmailAddress;

            svc["EmailAddress"].PropertyValue = Player.EmailAddress;

svc [" ForGame" ]。PropertyValue = Player.ForGame;

            svc["ForGame"].PropertyValue = Player.ForGame;

}

}

static public void SetPropertyValuesToDatabase( string userName, SettingsPropertyValueCollection svc)

static public void SetPropertyValuesToDatabase(string userName, SettingsPropertyValueCollection svc)

{

Guid userId = Guid (userName);

      Guid userId = new Guid(userName);

如果(! string .IsNullOrEmpty(userName))

      if (!string.IsNullOrEmpty(userName))

{

使用 TransactionScope ts = new TransactionScope ())

            using (TransactionScope ts = new TransactionScope())

{

个人玩家;

                  Individual Player;

var 玩家= dc.Individuals.Where (p => p.UserId == userId);

                  var Players = dc.Individuals.Where(p => p.UserId == userId);

如果(Players.Count()> 0)

                  if (Players.Count() > 0)

{

Player = Players.First();

}

else

{

Player = new 个人();

                        Player = new Individual();

Player.UserId = userId;

dc.Individuals.InsertOnSubmit(Player);

}

播放器.IsActive =( bool ?)svc [" IsActive" ]。PropertyValue;

                  Player.IsActive = (bool?)svc["IsActive"].PropertyValue;

Player.ForGame =(< span style ="color:#2b91af"> DateTime ?)svc [" ForGame" ]。PropertyValue == null ||

                  Player.ForGame = (DateTime?)svc["ForGame"].PropertyValue == null ||

(( DateTime ?)svc [" ForGame" ]。PropertyValue< = SqlDateTime .MaxValue.Value&&

                        ((DateTime?)svc["ForGame"].PropertyValue <= SqlDateTime.MaxValue.Value &&

DateTime ?)svc [" ; ForGame" ]。PropertyValue> = SqlDateTime .MinValue.Value)?

                        (DateTime?)svc["ForGame"].PropertyValue >= SqlDateTime.MinValue.Value) ?

DateTime ?)svc [" ForGame" ]。PropertyValue:

                        (DateTime?)svc["ForGame"].PropertyValue :

null ;

                        null;

Player.Nickname =( string )svc ["昵称"]。PropertyValue;

                  Player.Nickname = (string)svc["Nickname"].PropertyValue;

Player.EmailAddress =( string )svc [" EmailAddress" ]。PropertyValue;

                  Player.EmailAddress = (string)svc["EmailAddress"].PropertyValue;

dc.SubmitChanges();

}

}

}

推荐答案

也许微软可以提出建议?这是由微软开发人员监控的吗?

Maybe Microsoft can chime in with a suggestion?  Is this monitored by Microsoft devs?

谢谢,

Jason


这篇关于DuplicateKeyException:“无法添加具有已在使用的密钥的实体。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆