问题描述
我正在尝试一对一的关系,导航属性就在一边(MVC4,EF5,代码第一)。
I'm trying to do a one-to-one relationship, with the navigation property just on one side (MVC4, EF5, code first).
public class User {
public int UserId { get; set; } //PK
public int RankId { get; set; } //FK
public virtual Rank { get; set; } //Navigation
}
public class Rank {
public int RankId { get; set; }
}
配置:
public class RankConfiguration : EntityTypeConfiguration<Rank>
{
public RankConfiguration()
{
HasKey(e => e.RankId);
Property(e => e.RankId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration ()
{
// PK
HasKey(e => e.UserId);
Property(e => e.RankId)
.IsRequired();
HasRequired(e => e.Rank)
.WithRequiredDependent()
.WillCascadeOnDelete(true);
}
}
对于我看到的(对于我知道的小),db正确生成,但是我收到此错误:
For what I see (and for the little I know), db is correctly generated, but I get this error:
参考代码中的依赖属性映射到存储生成的列。列:'UserId'。
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'UserId'.
首先尝试创建一个连接表(参考文献),不知道是不是一个好主意,但我wasn不能用流畅的API来处理。
In a first try, I was thinking to create a join table (ref. here EF5 Fluent API one-to-one without navigator), don't know if was a good idea but I wasn't able to do with fluent API.
我不知道为什么,什么是错误的..任何帮助?非常感谢提前
I have no idea why and what is wrong .. any help? many thanks in advance
更新
首先尝试
在注释后,我尝试使用连接表配置一对一
First tryAfter @soadyp comment, I tried to configure a one-to-one with a join table
HasRequired(e => e.Rank)
.WithRequiredDependent()
.Map(m =>
{
m.ToTable("UserRank");
m.MapKey("RankId");
})
.WillCascadeOnDelete(true);
但我尝试迁移时收到此错误:
but I get this error when I try to migrate:
The specified table 'UserRank' was not found in the model.
Ensure that the table name has been correctly specified.
第二次尝试
可能我正在做一个简单的工作太复杂了,看完这篇我刚刚改变了这种方式
Second tryProbably I'm making a simple job too complicated, after reading this http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx I just changed in this way
HasRequired(e => e.Rank)
.WithOptional()
.WillCascadeOnDelete(true);
一切都很好,但是UserId PK没有设置为身份(当我插入时有明显的问题一排)。如果我指定为身份:
Everything is fine, BUT the UserId PK wasn't setted as Identity (with obvious problmes when I insert a row). If I specify it as Identity:
Property(e => e.UserId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
我收到以下错误:
Cascading foreign key 'FK_dbo.Users_dbo.Ranks_UserId' cannot be created where
the referencing column 'Users.UserID' is an identity column.
Could not create constraint. See previous errors.
推荐答案
好的,所以我已经复制了你的代码,并尝试了我的答案有多个部分:
OK, so i have copied your code and tried it and my answer has multiple parts:
- 使用您当前的配置,生成的数据库会弄乱一些如何,请参阅Users表中的UserId 正在成为FK(我不知道为什么真的)所以RankId只是成为一个正常的整数属性(它不是一个关键),所以我认为这是什么触发了你提到的关于参考证明的第一个异常,因为如果您考虑这个问题,数据库会知道UserId是主键,同时它是一个外键,但是UserId引用的实际键是RankId,不是数据库生成的是数据库假设想出所有这些信息。
- 现在可能有一个正确的配置来解决这个问题,但我找不到,所以为了解决这个问题,我删除了流畅的配置实体框架根据惯例和我创建了所有内容做了很好的工作(它发现User类中的RankId实际上是一个外键)。
尝试看看配置有什么问题,以及您使用SQL Server Management Studio或任何其他工具的每个更改都必须检查生成的数据库模式,以确保它是您希望的,加上如果不需要配置只是不要使用它。
An advice, try to see what's wrong with the configuration and with every change you make use SQL Server Management Studio or any other tool you have to check out the generated database schema to be sure it is what you wish it to be, plus if you don't need the configuration just don't use it.
这篇关于EF5一对一无导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!