首页新闻找找看学习计划

.net ef code first 外键如何配置成可空外键

0
悬赏园豆:15 [已关闭问题] 关闭于 2016-11-11 11:00

实体

[ForeignKey("juchuId")]
public virtual TFzm004 juchuTFzm004 { get; set; }

/// <summary>
/// JUCHU_ID
/// </summary>
[Column("JUCHU_ID", TypeName = "numeric")]
public virtual decimal? juchuId { get; set; }

 

 

关联实体

/// <summary>
/// TFcd008
/// </summary>
[ForeignKey("juchuId")]
public virtual IList<TFcd008> TFcd008s { get; set; }

 

 

实体已经配置成可空 为什么生成的Migrations就变成非空了 如下

public partial class test1 : DbMigration
{
public override void Up()
{
DropIndex("dbo.T_FCD008", new[] { "JUCHU_ID" });
AlterColumn("dbo.T_FCD008", "JUCHU_ID", c => c.Decimal(nullable: false, precision: 38, scale: 0, storeType: "numeric"));
CreateIndex("dbo.T_FCD008", "JUCHU_ID");
}

public override void Down()
{
DropIndex("dbo.T_FCD008", new[] { "JUCHU_ID" });
AlterColumn("dbo.T_FCD008", "JUCHU_ID", c => c.Decimal(precision: 38, scale: 0, storeType: "numeric"));
CreateIndex("dbo.T_FCD008", "JUCHU_ID");
}
}

 

问题补充:

手动改Migrations能实现 但是400多个表呀。。这个到底是什么引起的呢  用的ABP框架

15904915314的主页 15904915314 | 菜鸟二级 | 园豆:215
提问于:2016-11-11 10:48
< >
分享
所有回答(1)
0

兄弟你上面的

[ForeignKey("juchuId")]
public virtual IList<TFcd008> TFcd008s { get; set; }

外键属性标错了吧,我实践了一下,导航属性可以为空的:

话不多说,贴代码

评论实体:

public class Comment:CreationAuditedEntity<Guid,User>
{
public virtual string Text{get;set;}
public virtual string UserName { get; set; }
public virtual Question Question { get; set; }
public virtual Guid QuestionId { get; set; }

public virtual ICollection<Answer> AnswerList { get; set; }

public Comment()
{

}

public Comment(string Text)
{
this.Text = Text;
}
}

回复实体:

public class Reply: CreationAuditedEntity<Guid, User>
{
public virtual string Text { get; set; }

public virtual int Depth { get; set; }
[ForeignKey("CommentId")]
public virtual Comment Comment { get; set; }
public virtual Guid? CommentId { get; set; }
[ForeignKey("ReplyToId")]
public virtual Reply ReplyTo { get; set; }
public virtual Guid? ReplyToId { get; set; }
public virtual bool IsReply { get; set; }
public virtual ICollection<Reply> Replays { get; set; }
}

然后运行程序包控制台运行:

add-migration xxxxx

然后再运行:

update-database

然后结果:

兄弟,你再试试看吧

污小猪 | 园豆:206 (菜鸟二级) | 2018-05-28 15:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册