public class Parent
{
public virtual int Id{ get; set; }
public virtual ICollection<Child> Children { get; set; }
public Parent()
{
Children = new List<Child>();
}
}
public class Child
{
public virtual int Id{ get; set; }
public virtual string Name { get; set; }
}
映射:
public ParentMap()
{
Map(x => x.Number).Not.Nullable();
HasMany(x => x.Children)
.Cascade.None()
.KeyColumn("ParentId")
.ExtraLazyLoad()
.AsSet();
}
生成的Sql脚本:
ALTER TABLE [dbo].[Child] WITH CHECK ADD CONSTRAINT [FKFCFBAA864983AA6A] FOREIGN KEY([ParentId])
REFERENCES [dbo].[Parent] ([Id])
GO
ALTER TABLE [dbo].[Child] CHECK CONSTRAINT [FKFCFBAA864983AA6A]
GO
如何将[FKFCFBAA864983AA6A]
的外键格式更改为[FK_ChildTableName_ParentTableName]
?
答案 0 :(得分:0)
感谢@Rabban评论,.ForeignKeyConstraintName("FK_MyKey")
就是诀窍。
用于制图,
public ParentMap()
{
Map(x => x.Number).Not.Nullable();
HasMany(x => x.Children)
.Cascade.None()
.KeyColumn("ParentId")
.ForeignKeyConstraintName("FK_Children_Parent") //customize Foreign Key name
.ExtraLazyLoad()
.AsSet();
}