EF Code First外键无导航属性 [英] EF Code First foreign key without navigation property
问题描述
public class Parent
{
public int Id {get;组; }
}
public class Child
{
public int Id {get;组; }
public int ParentId {get;组; }
}
什么是代码第一流利的API语法来强制实现ParentId在数据库具有外键约束到Parents表,不需要有导航属性?
我知道如果我添加一个导航属性Parent to Child,然后我可以这样做:
modelBuilder.Entity< Child>()
.HasRequired< ;父>(c => c.Parent)
.WithMany()
.HasForeignKey(c => c.ParentId);
但是我不想在这种情况下导航属性。
使用EF Code First Fluent API是不可能的。您始终需要至少一个导航属性来创建数据库中的外键约束。
如果您使用代码优先迁移,您可以选择添加新的代码在包管理器控制台上的迁移( add-migration SomeNewSchemaName
)。如果您使用模型更改了某些内容或映射,则会添加新的迁移。如果没有更改任何内容,可以使用 add-migration -IgnoreChanges SomeNewSchemaName
来强制执行新的迁移。在这种情况下,迁移将仅包含空的向上
和 Down
方法。
然后,您可以通过添加以下内容来修改 Up
方法:
public override void Up()
{
//其他东西...
AddForeignKey(ChildTableName,ParentId,ParentTableName Id,
cascadeDelete:true); //或false
CreateIndex(ChildTableName,ParentId); //如果你想要一个索引
}
运行此迁移( update-database
在包管理控制台上)将运行类似于(对于SQL Server)的SQL语句:
CREATE INDEX [IX_SomeName] ON [ChildTableName]([ParentId])
或者,没有迁移,你可以运行一个纯SQL命令使用
context.Database.ExecuteSqlCommand(sql);
其中上下文
是您派生的一个实例上下文类和 sql
只是上面的SQL命令作为字符串。
注意,所有这个EF没有线索 ParentId
是描述关系的外键。 EF只会将其视为普通标量属性。不管怎么说,只是打开一个SQL管理工具并手动添加约束,所有上述都只是一个更复杂和更慢的方式。
Let's say I have the following entities:
public class Parent
{
public int Id { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
}
What is the code first fluent API syntax to enforce that ParentId is created in the database with a foreign key constraint to the Parents table, without the need to have a navigation property?
I know that if I add a navigation property Parent to Child, then I can do this:
modelBuilder.Entity<Child>()
.HasRequired<Parent>(c => c.Parent)
.WithMany()
.HasForeignKey(c => c.ParentId);
But I don't want the navigation property in this particular case.
With EF Code First Fluent API it is impossible. You always need at least one navigation property to create a foreign key constraint in the database.
If you are using Code First Migrations you have the option to add a new code based migration on the package manager console (add-migration SomeNewSchemaName
). If you changed something with your model or mapping a new migration will be added. If you didn't change anything force a new migration by using add-migration -IgnoreChanges SomeNewSchemaName
. The migration will only contain empty Up
and Down
methods in this case.
Then you can modify the Up
method by adding the follwing to it:
public override void Up()
{
// other stuff...
AddForeignKey("ChildTableName", "ParentId", "ParentTableName", "Id",
cascadeDelete: true); // or false
CreateIndex("ChildTableName", "ParentId"); // if you want an index
}
Running this migration (update-database
on package manage console) will run a SQL statement similar to this (for SQL Server):
ALTER TABLE [ChildTableName] ADD CONSTRAINT [FK_SomeName]
FOREIGN KEY ([ParentId]) REFERENCES [ParentTableName] ([Id])
CREATE INDEX [IX_SomeName] ON [ChildTableName] ([ParentId])
Alternatively, without migrations, you could just run a pure SQL command using
context.Database.ExecuteSqlCommand(sql);
where context
is an instance of your derived context class and sql
is just the above SQL command as string.
Be aware that with all this EF has no clue that ParentId
is a foreign key that describes a relationship. EF will consider it only as an ordinary scalar property. Somehow all the above is only a more complicated and slower way compared to just opening a SQL management tool and to add the constraint by hand.
这篇关于EF Code First外键无导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!