EF5一到一个没有导航 [英] EF5 one-to-one without navigation

查看:134
本文介绍了EF5一到一个没有导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个一对一的关系,与导航属性只是在一边(MVC4,EF5,代码在前)。

 公共类用户{

公众诠释用户ID {搞定;组; } // PK

公众诠释RankId {搞定;组; } // FK

公共虚拟排名{搞定;组; } //导航

}

公共类排名{

公众诠释RankId {搞定;组; }

}

配置:

 公共类RankConfiguration:EntityTypeConfiguration<&排名GT; 
{
公共RankConfiguration()
{
HasKey(E => e.RankId);
房产(E => e.RankId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
}
}

公共类UserConfiguration:EntityTypeConfiguration<使用者>
{
公共UserConfiguration()
{
// PK
HasKey(E => e.UserId);

房产(E => e.RankId)
.IsRequired();

HasRequired(E => e.Rank)
.WithRequiredDependent()
.WillCascadeOnDelete(真);
}
}

有关我所看到的(和小我知道),DB是正确生成,但我得到这个错误:



在ReferentialConstraint从属属性映射到一个存储生成列。专栏:'用户ID



在第一次尝试,我想创建一个连接表(参见这里的 EF5流利的API一到一个没有导航),不知道是否是个好主意,但我没'T能够用流利的API来完成。



我不知道为什么,什么是错..任何帮助吗?许多在此先感谢



更新



第一次尝试
@soadyp 的评论后,我想配置一到一个具有连接表

  HasRequired(E => e.Rank)
.WithRequiredDependent()
.MAP(M = GT ;
{
m.ToTable(UserRank);
m.MapKey(RankId);
})
.WillCascadeOnDelete(真);



但是当我尝试迁移我得到这个错误:

 指定的表'UserRank未在模型中发现。 
确保表名已指定正确。



二试
也许我做一个简单的工作太复杂了,阅读本的 http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part -3-共享主键-associations.aspx 我只是以这种方式改变了。

  HasRequired(E = > e.Rank)
.WithOptional()
.WillCascadeOnDelete(真);



一切都很好,但用户ID PK没有设置好的身份(与当我将明显problmes一排)。如果我将它指定为身份:

 属性(E => e.UserId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption。身份);



我收到以下错误:

 级联外键'FK_dbo.Users_dbo.Ranks_UserId无法创建其中
中的引用列'Users.UserID'是标识列。
无法创建约束。请参阅以前的错误。


解决方案

好了,我抄你的代码,试了一下我的回答有多个部分:




  1. 根据您当前的配置生成的数据库被弄乱了一些如何,请参阅用户表中的用户ID 正成为FK(我不知道为什么真的),所以RankId只是成为一个正常的整数属性(它不是一个键),所以我认为这是什么触发你提到的ReferentialConstraint第一个例外,因为如果你仔细想想,数据库知道用户ID是一个主键,并在同一时间,它是一个外键,但由用户ID,这是RankId引用的实际密钥没有生成的数据库那么如何在数据库想找出所有这些信息。

  2. 现在,有可能是解决这个问题一个正确的配置,但我找不到,所以要解决这个问题我删除了一口流利的配置和Entity Framework创建按约定的一切,它做的非常出色(它想通了,RankId中的User类实际上是一个外键)。



这是建议,尝试看看有什么错的配置和与每一个改变你使用SQL Server Management Studio或你必须检查出生成的数据库架构,以确保任何其他工具,它是什么,你希望它可以,再加上如果你不需要的配置只是不使用它。


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; }

}

Configuration:

 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);
    }
}

For what I see (and for the little I know), db is correctly generated, but I get this error:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'UserId'.

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

UPDATE

First try After @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 try Probably 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);

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);

I get the following error:

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:

  1. With your current configuration the generated database is messed up some how, see in the Users table the "UserId" is becoming the FK (i don't know why really) so "RankId" is just becoming a normal integer property (it is not a key), so i think this is what triggers the first exception you mentioned about the ReferentialConstraint, because if you think about it, the database knows that "UserId" is a primary key, and at the same time it is a foreign key but the actual key that is referenced by "UserId" which is "RankId" is not database generated so how is the database suppose to figure out all of this information.
  2. Now there might be a "right configuration to fix this but i couldn't find, so to solve this i removed the fluent configurations and Entity Framework created everything by convention and it did a great job (it figured out that "RankId" in the User class is actually a foreign key).

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一到一个没有导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆