允许空外键的GUID [英] Allow null foreign key GUID

查看:87
本文介绍了允许空外键的GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类型可为空的外键 GUID 像这样

  [ForeignKey的(CreatedBy)] 
[显示(NAME =创建者)]
公众的Guid? CreatedById {搞定;组; }

公共虚拟用户CreatedBy {搞定;组; }



但是,当我加入迁移和更新数据库不让它允许表设计空在SQL。



有另一种方法,使之允许通过模型第一?


解决方案<空/ DIV>

不知道为什么你不工作,或者如果你想通了。我们有几乎相同的设计,我们得到空的外键。我们通常使用流利的配置,但我们并没有对这些属性的任何配置为EF数字出来没有帮助。也许去除ForeignKey的属性可能会解决它。



  public虚拟用户CREATEUSER {搞定;组; } 
公众的Guid? CreateUserId {搞定;组; }


I want to create a nullable foreign key with type GUID like this

[ForeignKey("CreatedBy")]
[Display(Name = "Created by")]
public Guid? CreatedById { get; set; }

public virtual User CreatedBy { get; set; }

But when I add migration and update the database it doesn't make it allow null in table design in SQL.

Is there another way to make it allow null through model first ?

解决方案

Not sure why yours doesn't work or if you figured it out. We have pretty much the same design and we get nullable foreign keys. We typically use Fluent config but we don't have any config on these properties as EF figures them out without help. Maybe removing the ForeignKey attribute might fix it.

public virtual User CreateUser { get; set; }
public Guid? CreateUserId { get; set; }

这篇关于允许空外键的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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