English 中文(简体)
EF-代码-第一个多对多个相关天体生成两个不同的SQL表(MVC3)
原标题:EF-code-first many-to-many related objects generates two different SQL tables (MVC3)

我读了"https://stackoverflow.com/ questions/7050404/create-code-first-many-to-many-to-many-with-addication-first-many-many-first-many-to-first-fields-in-asssociation-able"[a]/a],我试图做类似的事情。我的模范班是:

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public ICollection<Course> CoursesAttending { get; set; }

    public Person()
    {
        this.CoursesAttending = new List<Course>();
    }
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }

    public ICollection<Person> Students { get; set; }
}

public class PersonCourse
{
    [Key, Column(Order = 0)]
    public int PersonID { get; set; }
    [Key, Column(Order = 1)]
    public int CourseID { get; set; }

    public virtual Person Student { get; set; }
    public virtual Course StudentCourse { get; set; }

    public int Mark { get; set; }
    public string Comment { get; set; }
}

public class SchoolContext : DbContext
{
    public DbSet<Course> Courses { get; set; }
    public DbSet<Person> People { get; set; }
    public DbSet<PersonCourse> PersonCourseLinks { get; set; }

    public SchoolContext()
        : base("ManyToManyTest")
    {
    }
}

现在,我试图增加一个新的人 并增加一个新的课程 在他的课程列表:

[HttpPost]
    public ActionResult Create(Person person)
    {
        if (ModelState.IsValid)
        {
            Course studentCourse;
            try
            {
                studentCourse = db.Courses.ToList<Course>().First();
            }
            catch
            {
                studentCourse = new Course() { Title = "HTML" };
            }

            person.CoursesAttending.Add(studentCourse);
            db.People.Add(person);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(person);
    }

一切顺利,但当我打开我创建的数据库时,我看到个人和课程班有两个链接表格――个人课程(包括:个人、课程、标记、评论)和人课程1(包括:个人、课程、说明),只有个人课程1 有行(实际上一行)。为什么发生?我做错了什么吗?我期望只看到一个链接表格――个人课程表......。

问题回答

我期望只看到一个链接表格 -- -- 个人课程表格

然后,您必须把personCourse 实体与person 实体连接起来,尽管 code>CourseAttend 导航属性。同样的事情也必须对 Code 实体进行。

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public ICollection<PersonCourse> CoursesAttending { get; set; }

    public Person()
    {
        this.CoursesAttending = new List<PersonCourse>();
    }
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }

    public ICollection<PersonCourse> Students { get; set; }
}

您在 Gerund 表格中给予外国密钥属性的外国密钥关联 :

public class PersonCourse
{
    [Key, Column(Order = 0)]
    public int PersonID { get; set; }
    [Key, Column(Order = 1)]
    public int CourseID { get; set; }

    [ForeignKey("PersonID")]
    public virtual Person Student { get; set; }

    [ForeignKey("CourseID")]
    public virtual Course StudentCourse { get; set; }

    public int Mark { get; set; }
    public string Comment { get; set; }
}

为何你的代码没有如预期的那样有效?

public class PersonCourse
{
    [Key, Column(Order = 0)]
    public int PersonID { get; set; }
    [Key, Column(Order = 1)]
    public int CourseID { get; set; }

    public virtual Person Person { get; set; } // Follow built in conventions
    public virtual Course Course { get; set; }

    public int Mark { get; set; }
    public string Comment { get; set; }
}

现在,这是继《实体框架公约》所建立的《实体框架公约》

public int [ClassName][Id] or [Id] //Database Foreign Key column

and

public NavigationProperty [ClassName] // Navigation Property




相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签