首先使用存储库模式插入第一组数据到EF代码。 [英] Insert First Set of Data To EF Code-First Using Repository Pattern.

查看:108
本文介绍了首先使用存储库模式插入第一组数据到EF代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Entity Framework Code-First和通用存储库模式将第一组数据插入数据库。我的POCO课程有这些关系:

i am trying to insert first set of data to database using Entity Framework Code-First And generic repository pattern. My POCO classes have these relations:


  • 学生与课程有多对多关系

  • 学生与拼贴有一对多的关系

这是我的POCO课程:

Here are my POCO classes:

public class Student : EntityBase
{
    public Student()
    {
        this.Lessons = new HashSet<Lesson>(); 
    }

    public string Name { get; set; }

    //FK Collage
    public int CollageId { get; set; }

    public virtual ICollection<Lesson> Lessons { get; set; }
    public virtual Collage Collages { get; set; }
}

public class Lesson : EntityBase
{
    public Lesson()
    {
        this.Students = new HashSet<Student>(); 
    }

    public string Name { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

public class Collage : EntityBase
{
    public Collage()
    {
        Students = new List<Student>(); 
    }
    public string Name { get; set; }


    public virtual ICollection<Student> Students { get; set; }

}

这是我的映射:

public class StudentMap : EntityTypeConfiguration<Student>
{
    public StudentMap()
    {
        //ToTable("TblStudent");

        HasRequired<Collage>(x => x.Collages)
            .WithMany(e => e.Students)
            .HasForeignKey(i => i.CollageId)
            .WillCascadeOnDelete(false);

        HasMany<Lesson>(x => x.Lessons)
            .WithMany(e => e.Students)
            .Map(i =>
            {
                i.MapLeftKey("StudentFKId");
                i.MapRightKey("LessonFKId");
                i.ToTable("TblStudent_Lesson");
            });

    }
}

现在在我的控制器中: p>

Now in my Controller:

public ActionResult Index()
{
    Repository<Student> _reStu = new Repository<Student>();
    _reStu.Add(new Student { Name = "Test", CollageId = 0, Collages = null, Lessons = null });
    _reStu.save();

    return View();
}

我收到此错误:


INSERT语句与FOREIGN KEY约束
FK_dbo.Students_dbo.TblCollage_CollageId冲突。冲突发生在
数据库test2,表dbo.TblCollage,列Id。陈述
已被终止。

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Students_dbo.TblCollage_CollageId". The conflict occurred in database "test2", table "dbo.TblCollage", column 'Id'. The statement has been terminated.

我知道我有一个表之间的关系,但是如何插入第一组的数据有什么关系?在我传递数据之后我应该包括我的关系吗?

I know that i have a relation between tables but how can i insert the first set of data when there is a relation? should i include my relation after i pass the data within?

推荐答案


当有关系时,如何插入第一组数据? >

how can i insert the first set of data when there is a relation?

CollageId 是不可空的列,这意味着您需要设置它。目前,您的模型表示,每个学生 必须有一个 College ,所以SQL Server不会允许您插入无效的行。

CollageId is a non-nullable column, which means that you need to set it. Currently your model says that every Student must have one College, so SQL Server will not allow you to insert an invalid row.

所以为了添加任何学生,你需要添加一个 College first。

So in order to add any Student you need to add a College first.


我应该在我传递数据后包含我的关系? p>

should i include my relation after i pass the data within?

EF很聪明,所以你应该可以使用这两种方法之一。

EF is pretty smart, so you should be able to go with either of these approaches.

在创建学生时,创建 College

Repository<Student> _reStu = new Repository<Student>();
_reStu.Add(new Student { Name = "Test", Collages = new College { Name = "TestCollege" }, Lessons = null });
_reStu.save();

或先创建 College ,然后学生

Repository<College> _reColl = new Repository<College>();
var college = new College
{
    Name = "TestCollege"
};
_reColl.Add(college)
_reColl.save();

// As long as you have the same reference your college will get an Id after you inserted it.

Repository<Student> _reStu = new Repository<Student>();
_reStu.Add(new Student { Name = "Test", CollegeId = college.Id, Lessons = null });
_reStu.save();

这篇关于首先使用存储库模式插入第一组数据到EF代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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