实体框架 - 定义不需要侧面的连接 [英] Entity framework - Define connection where neither side is required
问题描述
模型目前看起来像这样:
public class授权
{
public int AuthorizationID {get;组; }
public virtual Change Change {get;组;
}
public class更改
{
public int ChangeID {get;组; }
public virtual授权授权{get;组; }
public int?授权ID {get;组; }
}
原因是我们可以独立于更改和一些更改需要授权,有些不需要,所以双方都不需要关系。
我可以使用流畅的API来配置,如下所示:
modelBuilder.Entity< Authorization>()
.HasOptional(t => t.Change)
.WithOptionalPrincipal t => t.授权);
很好,除了它创建的迁移看起来像这样
CreateTable(
dbo.Changes,
c => new
{
ChangeID = c。 int(nullable:false,identity:true),
AuthorizationID = c.Int(),
Authorisation_AuthorisationID = c.Int(),
})
.PrimaryKey(t = > t.ChangeID)
.ForeignKey(dbo.Authorisations,t => t.Authorisation_AuthorizationID)
.Index(t => t.Authorisation_AuthorizationID);
EF正在决定要添加新列( Authorisation_AuthorisationID )为了使我作为两个实体之间的FK,我真正想要做的就是将change.AuthorizationID属性作为FK使用到Authorization,我找不到一种配置方式(请注意,我需要FK在模型中 - 与其他应用程序的一致性比其他任何东西一样)。
总而言之,我需要能够创建一个关系双方之间的关系是可选的,如果可能,我希望能够自己定义FK列。
我只是接近这个错误?我一直在盯着同一个代码块,这么长时间我可能会丢失一些简单的东西。
看起来像显式的外键属性不支持一对一
关系 - 没有 HasForeignKey
Fluent API,如果你把 ForeignKey
属性导航属性你得到异常说多重性必须是 *
。
所以唯一的选择是删除显式的 Change.AuthorizationID
属性,只能使用导航属性:
型号:
public class授权
{
public int AuthorizationID {get;组; }
public virtual Change Change {get;组;
}
public class更改
{
public int ChangeID {get;组; }
public virtual Authorization {get;组;
}
配置:
modelBuilder.Entity< Authorization>()
.HasOptional(t => t.Change)
.WithOptionalPrincipal(t = > t.Authorisation)
.Map(a => a.MapKey(AuthorizationID));
I have a problem, I have to create a model where we have two entities which CAN be linked together but can also exist as stand alone entities.
The model currently looks like this:
public class Authorisation
{
public int AuthorisationID { get; set; }
public virtual Change Change { get; set; }
}
public class Change
{
public int ChangeID{ get; set; }
public virtual Authorisation Authorisation { get; set; }
public int? AuthorisationID{ get; set; }
}
The reason for this is that we can have an authorization record independent of a change, and some changes require authorisation and some dont, so neither side of the relationship is required.
I can configure this with the fluent API like so:
modelBuilder.Entity<Authorisation>()
.HasOptional(t => t.Change)
.WithOptionalPrincipal(t => t.Authorisation);
And alls well, Except that the migration that it creates looks like this
CreateTable(
"dbo.Changes",
c => new
{
ChangeID = c.Int(nullable: false, identity: true),
AuthorisationID = c.Int(),
Authorisation_AuthorisationID = c.Int(),
})
.PrimaryKey(t => t.ChangeID)
.ForeignKey("dbo.Authorisations", t => t.Authorisation_AuthorisationID)
.Index(t => t.Authorisation_AuthorisationID);
EF is deciding that its going to add a new column (Authorisation_AuthorisationID) for me to use as the FK between the two entities, what I really want to be able to do is to use the change.AuthorisationID property as the FK onto the Authorisation, I cannot find a way to configure this at all (Please note that I need the FK to be in the model - for consistency with the rest of the app more than anything else).
To sum up I need to be able to create a relationship between two entities where both sides of the relationship are optional and if possible I want to be able to define the FK column myself.
Am I just approaching this wrong? Ive been staring at the same block of code for so long I could be missing something simple.
Looks like explicit foreign key property is not supported for one-to-one
relationships - there is no HasForeignKey
Fluent API and also if you put ForeignKey
attribute on the navigation property you get exception saying that multiplicity must be *
.
So the only choice you have is to remove the explicit Change.AuthorisationID
property and work only with navigation properties:
Model:
public class Authorisation
{
public int AuthorisationID { get; set; }
public virtual Change Change { get; set; }
}
public class Change
{
public int ChangeID{ get; set; }
public virtual Authorisation Authorisation { get; set; }
}
Configuration:
modelBuilder.Entity<Authorisation>()
.HasOptional(t => t.Change)
.WithOptionalPrincipal(t => t.Authorisation)
.Map(a => a.MapKey("AuthorisationID"));
这篇关于实体框架 - 定义不需要侧面的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!