1
我有這樣的實體:衝突在集合屬性與現有的價值實體PRIMARY KEY的
public class Partner
{
public string ID { get; set; }
public string Name { get; set; }
public string PersonId { get; set; }
public int ProjectId { get; set; }
#region Navigation Properties
public virtual Person Person { get; set; }
public virtual Project Project { get; set; }
#endregion
}
public class Person
{
public string ID { get; set; }
public string Username { get; set; }
#region Navigation Properties
public virtual ICollection<Partner> Partners { get; set; }
#endregion
}
public class Project
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DateStart { get; set; }
public DateTime DateEnd { get; set; }
public string Client { get; set; }
#region Navigation Properties
public ICollection<Partner> Partners { get; set; }
#endregion
}
當我插入一個新的項目,我在Partners
財產(Project
類)現有的合作伙伴,或者Person
財產(Partner
類)現有人員。發生這種情況時,會發生「違反主密鑰」的例外情況。
我的代碼SABE Projects
是這樣的:
//GetAllProject return IList<ProjectModel>
var projects = this.findProjectService.GetAllProjects();
foreach(var project in projects)
{
var projectDb = context.Project.Where(e => e.Id == project.Id).FirstOrDefault();
if (projectDb == null)
{
logger.Debug("Try add new project {0}", project.Name);
var newProject = Mapper.Map<ProjectModel, Project>(project);
context.Project.Add(newProject);
context.SaveChanges();
}
}
我如何避免這個問題?
編輯爲包括映射:
internal PartnerMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.IsUnicode(false);
this.Ignore(t => t.Name);
this.Property(t => t.PersonId)
.IsUnicode(false);
// Table
this.ToTable("Partner");
// Relations
this.HasRequired(t => t.Project)
.WithMany(r => r.Partners)
.HasForeignKey(t => t.ProjectId)
.WillCascadeOnDelete(false);
this.HasRequired(t => t.Person)
.WithMany(r => r.Partners)
.HasForeignKey(t => t.PersonId)
.WillCascadeOnDelete(false);
}
internal PersonMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.IsUnicode(false);
this.Ignore(t => t.Username);
// Table
this.ToTable("Person");
// Relations
}
internal ProjectMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Name)
.IsUnicode(false);
this.Property(t => t.Client)
.IsUnicode(false);
// Table
this.ToTable("Project");
// Relations
}
你能展示automapper的映射設置和key的ef數據註解嗎? –
您是否在SaveChanges之前檢查newProject.Partners中的'PersonId'和'ProjectId'?這些應該是默認值(null,0)或者與數據庫中的相同。 – nrodic
@SherifAhmed是的,我包括映射 –