代码第一实体框架虚拟和外键混淆 [英] Code First Entity Framework Virtual and Foreign Key Confusions

查看:126
本文介绍了代码第一实体框架虚拟和外键混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些POCO课程:

  public class Device:DbEntity 
{
public string DeviceName {get; set;}
}
public class AUT:DbEntity
{
public string ApplicationName {get; set;}
}
public class设置:DeviceAppDbEntity
{
public bool Worked {get; set;}
}

public class DbEntity
{
public Guid ID { get; set;}
}
public class DeviceAppDbEntity:DbEntity
{
public virtual AUT ApplicationUnderTesting {get; set;}
public virtual Device DeviceUnderTesting {get; set ;}
}

所以如果我想删除AUT(应用程序),那么我将几乎可以肯定得到一个错误,因为安装程序有一个外键到App。我被告知,如果你添加了

  public class DeviceAppDbEntity:DbEntity 
{
public virtual AUT ApplicationUnderTesting {get; set;}
public Guid ApplicationUnderTestingId {get; set;} //此行添加

public virtual Device DeviceUnderTesting {get; set;}
}

然后级联删除将被启用。这是否正确?



当我现在访问设置

  _repository .Setups.FirstOrDefault(x => x.ID.Equals(setupID)); 

导航属性 virtual AUT & 虚拟设备应该自动填写?将自动填写 Guid ApplicationUnderTestingId



当我要保存我的安装实体,那么我可以填写nav属性吗?或者我必须填写 Guid ApplicationUnderTestingId 字段,或者我做相反的,只填写 Guid ApplicationUnderTestingId

  entityToSave.AUT = _repository.AUTs.FirstOrDefault(x => x.ID.Equals(vm。的AppID)); 

  entityToSave.ApplicationUnderTestingId = vm.AppID; 
entityToSave.AUT = _repository.AUTs.FirstOrDefault(x => x.ID.Equals(vm.AppID));

  entityToSave.ApplicationUnderTestingId = vm.AppID; 

这有点混乱..当你定义一个 List&实体> ,因为当您删除包含列表的实体时。它抱怨列表中的实体有关系,如何启用级联删除?

  public class Blog:DbEntity 
{
public string Name {get; set;}
public virtual Member {get; set;}

public List< Comment>评论{get; set;}
}

我知道这似乎是很多问题,但它围绕实体框架中的一个主题。感谢您随时回答我的问题。

解决方案

回答这个。 / p>

Cascade On Delete 在您执行导航属性(例如

  public virtual SomeClass {get; set;} 
public Guid SomeClassId {get; set;}

您可以填写其中的任何一个,并将其保存到数据库。



没有级联删除

  public virtual SomeClass SomeClass {get; set;} 

当您从数据库中读取时,我会像下面这样做:

  var item = _repository.SomeClasses.FirstOrDefault(x => x.Id.Equals(id)); 

导航属性只有在您想要访问时才会被填写,因为我使用关键字 virtual



至于列表< Post> 去,如果你这样做:

  public class Post 
{
public virtual Blog Blog {get ; set;}
public Guid BlogId {get; set;}
}

然后当一个博客被删除时,与它相关联的所有帖子都将被删除。我没有试图看到如果您删除 BlogId 会发生什么,但我猜,删除博客时,不会删除任何帖子。 Blog =>发布模型不是示范您为什么要这样做的模式。



我想我已经回答了我所有的问题。总而言之,这一切取决于 public Guid SomeClassId {get; set;} 关于 Cascade On Delete 是否设置。


So I have some POCO classes:

public class Device : DbEntity
{
  public string DeviceName {get;set;}
}
public class AUT : DbEntity
{
  public string ApplicationName {get;set;}
}
public class Setup : DeviceAppDbEntity
{
  public bool Worked {get;set;}
}

public class DbEntity
{
  public Guid ID {get;set;}
}
public class DeviceAppDbEntity : DbEntity
{
  public virtual AUT ApplicationUnderTesting {get;set;}
  public virtual Device DeviceUnderTesting {get;set;}
}

So if I want to delete an AUT (Application) then I will almost certainly get an error because Setup has a foreign key to App. I've been told that if you add

public class DeviceAppDbEntity : DbEntity
{
  public virtual AUT ApplicationUnderTesting {get;set;}
  public Guid ApplicationUnderTestingId {get;set;} // THIS LINE ADDED

  public virtual Device DeviceUnderTesting {get;set;}
}

Then cascade delete will be enabled. Is this correct?

When I now access the Setups

_repository.Setups.FirstOrDefault(x => x.ID.Equals(setupID));

The navigation property virtual AUT & virtual Device should be automatically filled out?? Will the Guid ApplicationUnderTestingId be automatically filled out?

When I want to save my Setup entity, then can I just fill out the nav property? or do I have to fill out the Guid ApplicationUnderTestingId field as well, or do I do the reverse and only fill out the Guid ApplicationUnderTestingId?

entityToSave.AUT = _repository.AUTs.FirstOrDefault(x => x.ID.Equals(vm.AppID));

or

entityToSave.ApplicationUnderTestingId= vm.AppID;
entityToSave.AUT = _repository.AUTs.FirstOrDefault(x => x.ID.Equals(vm.AppID));

or

entityToSave.ApplicationUnderTestingId= vm.AppID;

This is kinda confusing.. It gets worse when you define a List<entities>, because when you delete the entity which contains the list. It complains about the entities within the list having relationships, how do you enable cascade delete on these?

public class Blog: DbEntity
{
  public string Name {get;set;}
  public virtual Member {get;set;}

  public List<Comment> Comments {get;set;}
}

I know it seems like a lot of questions, but it revolves around one topic in entity framework.Thank you for anytime spent answering my questions.

解决方案

To answer this.

Cascade On Delete is enable when you do navigation properties such as

public virtual SomeClass {get;set;}
public Guid SomeClassId {get;set;}

You can fill out either of them and it will save to the database.

No Cascade On Delete

public virtual SomeClass SomeClass {get;set;}

When you read from the database I do mine like below:

var item = _repository.SomeClasses.FirstOrDefault(x => x.Id.Equals(id));

The navigation properties will only be filled out when you want to access them, lazy loading, because I used the keyword virtual

As far as the List<Post> goes, if you do:

public class Post
{
    public virtual Blog Blog {get;set;}
    public Guid BlogId {get;set;}
}

Then when a blog is deleted, all the posts associated with it will be deleted. I haven't tried to see what happens if you remove BlogId, but I'm guessing, when you delete a blog then none of the posts will be deleted. The Blog => Post model isn't the model to demonstrate why you would want to do this though.

I think I have answered all my questions. To sum up though, it all depends on public Guid SomeClassId {get;set;} as to whether Cascade On Delete is set or not.

这篇关于代码第一实体框架虚拟和外键混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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