实体框架代码首先:使用注释设置一对一外键关联 [英] Entity Framework Code First : Setting up One-To-One foreign key association using Annotations
问题描述
我有以下两个实体,我正在尝试使用外键关联(一对一)。
I have following two Entities that I am trying to relate (one to one) using foreign key associations.
public class StandardRack {
public int Id {get;set}
public StandardRelay StandardRelay {get;set}
}
public class StandardRelay {
public int Id {get;set}
public int StandardRack_Id {get;set;}
[Required][ForeignKey("StandardRack_Id")]
public StandardRack StandardRack { get; set; }
}
这会引发ModelValidationException。任何想法为什么不能配置这样一个简单的一对一双向关系。
This throws ModelValidationException. Any ideas why such a seemingly simple one-to-one bidirectional relationship cannot be configured.
编辑:
这是例外:
System.Data.Entity.ModelConfiguration.ModelValidationException被捕获
消息=在模型生成期间检测到一个或多个验证错误:
System.Data.Entity.ModelConfiguration.ModelValidationException was caught Message=One or more validation errors were detected during model generation:
System.Data.Edm.EdmAssociationEnd::多重性在关系StandardRelay_StandardRack中的角色StandardRelay_StandardRack_Source中无效。因为从属角色属性不是关键属性,因此依赖角色的多重性的上限必须是 * 。
System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'StandardRelay_StandardRack_Source' in relationship 'StandardRelay_StandardRack'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be �*�.
Source = EntityFramework
StackTrace :
在System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel模型,XmlWriter writer)
在System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel模型)
在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
在System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
在System.Data.Entity.Internal。 LazyInternalContext.CreateModel(LazyInternalContext internalContext)
在System.Data.Entity.Internal.RetryLazy 2.GetValue(TInput input)
1.Initialize()
在System.Data.Entity.Internal.LazyInternalContext.InitializeContext ()
at System.Data.Entity.Internal.InternalContext.Initialize()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
在System.Data.Entity.Internal.Linq.InternalSet
在System.Data.Entity.Internal.Linq.InternalSet 1.GetEnumerator()
1.System.Collections.Generic.IEnumerable.GetEnumerator()
在System.Data.Entity.Infrastructure .DbQuery
在System.Collections.Generic.List 1..ctor(IEnumerable
1 collection)
在System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)
在TestApplication.MainWindow.Window_Loaded(对象发件人,RoutedEventArgs e)在D:\RailwayProjects \RelayAnalysis\TestApplication\MainWindow.xaml.cs:line 33
InnerException:
Source=EntityFramework
StackTrace:
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateCsdl(EdmModel model)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet
1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator()
at System.Collections.Generic.List1..ctor(IEnumerable
1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TestApplication.MainWindow.Window_Loaded(Object sender, RoutedEventArgs e) in D:\RailwayProjects\RelayAnalysis\TestApplication\MainWindow.xaml.cs:line 33
InnerException:
推荐答案
我认为外键应该是Id,而不是StandardRa ck_id。
此外,您应该使用虚拟的,以便能够使用延迟加载。
I think the foreignKey should be Id, not StandardRack_id. Also, you should use virtual, in order to be able to use lazy loading.
这适用于我
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace Racks
{
public class StandardRack
{
public int Id { get; set; }
public virtual StandardRelay StandardRelay { get; set; }
}
public class StandardRelay
{
public int Id { get; set; }
public int StandardRack_Id { get; set; }
[ForeignKey("Id")]
[Required]
public virtual StandardRack StandardRack { get; set; }
}
public class Context : DbContext
{
static Context()
{
Database.SetInitializer<Context>(null);
}
public DbSet<StandardRack> StandardRacks { get; set; }
public DbSet<StandardRelay> StandardRelays { get; set; }
}
class Program
{
static void Main(string[] args)
{
var context = new Context();
context.Database.Delete();
context.Database.Create();
var standardRack = new StandardRack();
standardRack.StandardRelay = new StandardRelay();
context.StandardRacks.Add(standardRack);
context.SaveChanges();
}
}
}
这篇关于实体框架代码首先:使用注释设置一对一外键关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!