c# – 实体框架:多对多关系中的重复记录

c# – 实体框架:多对多关系中的重复记录,第1张

概述我有以下实体框架代码第一个代码.创建表并插入数据.但是俱乐部表中有重复的记录. 我的业务是: >使用俱乐部创建应用程序创建俱乐部 >使用人物应用程序创建人物 如何避免重复输入? static void Main(string[] args) { Database.SetInitializer<NerdDinners>(new MyInitializer()); 我有以下实体框架代码第一个代码.创建表并插入数据.但是俱乐部表中有重复的记录.

我的业务是:

>使用俱乐部创建应用程序创建俱乐部
>使用人物应用程序创建人物

如何避免重复输入?

static voID Main(string[] args)    {        Database.Setinitializer<NerdDinners>(new MyInitializer());        CreateClubs();        InsertPersons();    }    public static voID CreateClubs()    {        string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";        using (var db = new NerdDinners(connectionstring))        {            Club club1 = new Club();            club1.Clubname = "club1";            Club club2 = new Club();            club2.Clubname = "club2";            Club club3 = new Club();            club3.Clubname = "club3";            db.Clubs.Add(club1);            db.Clubs.Add(club2);            db.Clubs.Add(club3);            int recordsAffected = db.SaveChanges();        }    }    public static Club GetClubs(string clubname)    {        string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";        using (var db = new NerdDinners(connectionstring))        {            //var query = db.Clubs.Where(p => p.Clubname == clubname);            var query = db.Clubs.SingleOrDefault(p => p.Clubname == clubname);            return query;        }    }    public static voID InsertPersons()    {        string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";        using (var db = new NerdDinners(connectionstring))        {            Club club1 = GetClubs("club1");            Club club2 = GetClubs("club2");            Club club3 = GetClubs("club3");            Person p1 = new Person();            p1.Personname = "Person1";            Person p2 = new Person();            p2.Personname = "Person2";            List<Club> clubsForPerson1 = new List<Club>();            clubsForPerson1.Add(club1);            clubsForPerson1.Add(club3);            List<Club> clubsForPerson2 = new List<Club>();            clubsForPerson2.Add(club2);            clubsForPerson2.Add(club3);            p1.Clubs = clubsForPerson1;            p2.Clubs = clubsForPerson2;            db.Persons.Add(p1);            db.Persons.Add(p2);            int recordsAffected = db.SaveChanges();        }    }

public class Person{    public int PersonID { get; set; }    public string Personname { get; set; }    public virtual ICollection<Club> Clubs { get; set; }}public class Club{    public int ClubID { get; set; }    public string Clubname { get; set; }    public virtual ICollection<Person> Members { get; set; }}//System.Data.Entity.DbContext is from EntityFramework.dllpublic class NerdDinners : System.Data.Entity.DbContext{    public NerdDinners(string connString): base(connString)    {     }    protected overrIDe voID OnModelCreating(DbModelBuilder modelbuilder)    {         //Fluent API - Plural Removal        modelbuilder.Conventions.Remove<PluralizingtablenameConvention>();    }    public DbSet<Person> Persons { get; set; }    public DbSet<Club> Clubs { get; set; }}
解决方法 问题是您创建更多的上下文.

首先你创建俱乐部.没关系.但是,当您创建人员时,您可以通过GetClubs获取俱乐部,但是对于每个俱乐部,您将处理实际的实体框架上下文,以便最终分离实体.在InsertPersons,你添加了离散的俱乐部实体给新的人,所以实际上下文会认为俱乐部是新的俱乐部.

所以当你为一个人添加一个俱乐部,你实际上创建了新的俱乐部.

这是因为实体框架跟踪更改并管理每个上下文的实体.如果您将实体添加到不包含它的上下文中,那么它将像新实体一样对待.

其实你应该这样做(未测试):

static voID Main(string[] args){    Database.Setinitializer<NerdDinners>(new MyInitializer());    string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";    using (var db = new NerdDinners(connectionstring))    {        CreateClubs(db);        InsertPersons(db);    }}public static voID CreateClubs(NerdDinners db){    Club club1 = new Club();    club1.Clubname = "club1";    Club club2 = new Club();    club2.Clubname = "club2";    Club club3 = new Club();    club3.Clubname = "club3";    db.Clubs.Add(club1);    db.Clubs.Add(club2);    db.Clubs.Add(club3);    int recordsAffected = db.SaveChanges();}public static Club GetClubs(string clubname,NerdDinners db){    //var query = db.Clubs.Where(p => p.Clubname == clubname);    var query = db.Clubs.SingleOrDefault(p => p.Clubname == clubname);    return query;}public static voID InsertPersons(NerdDinners db){    Club club1 = GetClubs("club1",db);    Club club2 = GetClubs("club2",db);    Club club3 = GetClubs("club3",db);    Person p1 = new Person();    p1.Personname = "Person1";    Person p2 = new Person();    p2.Personname = "Person2";    List<Club> clubsForPerson1 = new List<Club>();    clubsForPerson1.Add(club1);    clubsForPerson1.Add(club3);    List<Club> clubsForPerson2 = new List<Club>();    clubsForPerson2.Add(club2);    clubsForPerson2.Add(club3);    p1.Clubs = clubsForPerson1;    p2.Clubs = clubsForPerson2;    db.Persons.Add(p1);    db.Persons.Add(p2);    int recordsAffected = db.SaveChanges();}

当然,你应该重构这个代码的结构,但请注意,我的 *** 作只使用一个EF上下文.

总结

以上是内存溢出为你收集整理的c# – 实体框架:多对多关系中的重复记录全部内容,希望文章能够帮你解决c# – 实体框架:多对多关系中的重复记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存