使用实体框架在一个表中包含2个外键 [英] 2 foreign keys in one table using entity framework

查看:102
本文介绍了使用实体框架在一个表中包含2个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个课程

 事件,狗,结果
因此必须表示我的结果类必须包含2个外键。 1表示事件,1表示狗。我对如何进行设置感到非常困惑。我已经在此方面获得了Som的帮助,希望有人能再次为我提供帮助。



现在这是我的课程:



公共类Dog
{

  public int DogId {get;组; } 
公共字符串Name {get;组; }
public int年龄{get;组; }
public bool已选中{get;组; }
公共字符串DogImage {get;组; }

[ForeignKey( Event)]
public int EventID {get;组; }


公共虚拟ICollection< Result>结果{get;组; }

公共虚拟事件Event {get;组; }
}

public class Event
{
public int EventId {get;组; }
公共字符串EventName {get;组; }
公共字符串EventLocation {get;组; }
公共字符串EventType {get;组; }
公共字符串EventDate {get;组; }

公共虚拟ICollection< Dog>狗{组; }
}

公共类结果
{
public int ResultId {get;组; }
public int Track {get;组; }
public int服从{get;组; }
public int Protection {get;组; }

[ForeignKey( Dog)]
public int DogId {get;组; }

公共虚拟Dog Dog {get;组; }
}

感谢帮助!



编辑:



好的,这就是现在的样子。我应该告诉您,当我尝试对这些更新进行管理时,收到一个以结尾的错误:可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。



我在dbcontext中添加了此方法,关心这个问题(我认为...)

 公共类EfdbContext:DbContext 
{
public DbSet< Event>活动{组; }
public DbSet< Dog>狗{组; }
public DbSet< Result>结果{get;组; }

受保护的覆盖无效OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Event>()
.HasRequired(c => c.Results)
.WithMany()
.WillCascadeOnDelete(false);
}

现在,当我尝试迁移时,出现此错误:


ALTER TABLE语句与FOREIGN KEY约束 FK_dbo.Events_dbo.Results_Results_ResultId冲突。数据库 CloudDog.EFDB.EfdbContext的表 dbo.Results的 ResultId列中发生了冲突。
这是我的班级现在的样子:




 公共课活动
{
public int EventId {组; }
公共字符串EventName {get;组; }
公共字符串EventLocation {get;组; }
公共字符串EventType {get;组; } // Dropdown medtävlingar
public string EventDate {get;组; } //选择器

公共虚拟ICollection< Dog>狗{组; }
公共虚拟ICollection< Result>结果{get;组; }
}

公共类Dog
{
public int DogId {get;组; }
公共字符串Name {get;组; }
public int年龄{get;组; }
public bool已选中{get;组; }
公共字符串DogImage {get;组; }

[ForeignKey( Event)]
public int EventId {get;组; }

公共虚拟ICollection< Merit>优点组; }
公共虚拟ICollection< Result>结果{get;组; }

公共虚拟事件Event {get;组; }
}

公共类结果
{
public int ResultId {get;组; }
public int Track {get;组; }
public int服从{get;组; }
public int Protection {get;组; }

[ForeignKey( Dog)]
public int DogId {get;组; }
公共虚拟Dog Dog {get;组; }

[ForeignKey( Event)]
public int EventId {get;组; }
公共虚拟事件Event {get;组; }
}


解决方案

在您的上一个问题,您并没有讲完整的故事。就其本身而言,解决方案是正确的。当两个类之间没有多对多的关联时,EF可以使用在类模型中仍然不可见的联结表对此建模。



但是现在您将更多信息添加到此联结表中。这意味着该表必须在类模型中表示为类,否则您将永远无法访问此信息。现在您的模型应该看起来像这样(简化为要点):

 公共类Dog 
{
public int DogId {get;组; }
公共虚拟ICollection< Result>结果{get;组; }
}

public class Event
{
public int EventId {get;组; }
公共虚拟ICollection< Result>结果{get;组; }
}

公共类结果
{
public int ResultId {get;组; }

[ForeignKey( Dog)]
public int DogId {get;组; }
公共虚拟Dog Dog {get;组; }

[ForeignKey( Event)]
public int EventId {get;组; }
个公共虚拟狗事件{get;组; }
}

逻辑上,<$ c之间仍然存在多对多关联$ c> Dog 和 Event ,但是技术上将其实现为1-n-1关联。因此,通过添加信息,您牺牲了从 Dog s到 Event s以及之后的快捷访问。这是很常见的。我很少看到纯粹的多对多关联。人们迟早会开始存储有关该关联的信息。



现在,如果您想获取 Dog Event s,您必须编写一个更为复杂的查询:

 从d的狗
从d的r。结果
选择r。事件


I have three classes

Event, Dog, Result

Each dog can participate in many events and for each event he will get a score related to this specifik event. Thus must mean that my result-class must contain 2 foreign keys. 1 that specifies the event and 1 that specifies the dog. I am very confused about how to set this up. I´ve been getting som help with this earlier and I hope someonen can help me again:

Here are my classes now:

public class Dog {

    public int DogId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Checked { get; set; }
    public string DogImage { get; set; }

    [ForeignKey("Event")]
    public int EventID { get; set; }


    public virtual ICollection<Result> Results { get; set; }

    public virtual Event Event { get; set; }
}

public class Event
    {
        public int EventId { get; set; }
        public string EventName { get; set; }
        public string EventLocation { get; set; }
        public string EventType { get; set; }
        public string EventDate { get; set; } 

        public virtual ICollection<Dog> Dogs { get; set; }
    }

 public class Result
    {
        public int ResultId { get; set; }
        public int Track { get; set; }
        public int Obedience { get; set; }
        public int Protection { get; set; }

        [ForeignKey("Dog")]
        public int DogId { get; set; }

        public virtual Dog Dog { get; set; }
    }

Help appreciated, thanks!

EDIT:

Ok, this is how it looks now. I should tellyou that when i tried to do a mgration with these updates I received an error ending with: may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I added this method in my dbcontext and it took care of that provblem(I Think...)

public class EfdbContext : DbContext
{
    public DbSet<Event> Events { get; set; }
    public DbSet<Dog> Dogs { get; set; }
    public DbSet<Result> Results { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Event>()
    .HasRequired(c => c.Results)
    .WithMany()
    .WillCascadeOnDelete(false);
} 

Now when I try to migrate I get this error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Events_dbo.Results_Results_ResultId". The conflict occurred in database "CloudDog.EFDB.EfdbContext", table "dbo.Results", column 'ResultId'. And this is how my classes look now:

public class Event
{
    public int EventId { get; set; }
    public string EventName { get; set; }
    public string EventLocation { get; set; }
    public string EventType { get; set; } //Dropdown med tävlingar
    public string EventDate { get; set; } //Picker

    public virtual ICollection<Dog> Dogs { get; set; }
    public virtual ICollection<Result> Results { get; set; }
}

public class Dog
{
    public int DogId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Checked { get; set; }
    public string DogImage { get; set; }

    [ForeignKey("Event")]
    public int EventId { get; set; }

    public virtual ICollection<Merit> Merits { get; set; }
    public virtual ICollection<Result> Results { get; set; }

    public virtual Event Event { get; set; }
}

 public class Result
{
    public int ResultId { get; set; }
    public int Track { get; set; }
    public int Obedience { get; set; }
    public int Protection { get; set; }

    [ForeignKey("Dog")]
    public int DogId { get; set; }
    public virtual Dog Dog { get; set; }

    [ForeignKey("Event")]
    public int EventId { get; set; }      
    public virtual Event Event { get; set; }
}

解决方案

In your previous question you didn't tell the whole story. In itself the solution was correct. When there is nothing but a many-to-many association between two classes then EF can model this by a junction table that remains invisible in the class model.

But now you add more information to this junction table. This means that the table must be represented as a class in the class model, otherwise you'll never be able to access this information. Now your model should look like this (reduced to the essentials):

public class Dog
{
    public int DogId { get; set; }
    public virtual ICollection<Result> Results { get; set; }
}

public class Event
{
    public int EventId { get; set; }
    public virtual ICollection<Result> Results { get; set; }
}

public class Result
{
    public int ResultId { get; set; }

    [ForeignKey("Dog")]
    public int DogId { get; set; }
    public virtual Dog Dog { get; set; }

    [ForeignKey("Event")]
    public int EventId { get; set; }
    public virtual Dog Event { get; set; }
}

Logically, there's still a many-to-many association between Dog and Event, but technically it is implemented as a 1-n-1 association. So by adding information you sacrifice the shortcut access from Dogs to Events and back. This is very common though. I don't often see pure many-to-many associations. Sooner or later people start storing information about the association.

Now if you want to fetch a Dog's Events, you'll have to write a somewhat more complicated query:

from d in Dogs
from r in d.Results
select r.Event

这篇关于使用实体框架在一个表中包含2个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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