简体   繁体   English

如何使用Entity Framework 4.1 Code First强制数据库中一对多关系的一对一关系

[英]How to use Entity Framework 4.1 Code First to force a one to one relationship for a one to many relationship in the database

I am trying to change EF1 for EF4.1 code first in an application where the schema cannot be changed because it is used in SQL Server Replication. 我试图在一个无法更改架构的应用程序中首先更改EF1 for EF4.1代码,因为它在SQL Server Replication中使用。 The schema is dreadfully poor and in many places describes relationships completely back to front. 架构非常糟糕,并且在许多地方描述完全回到前面的关系。

What I am trying to do is create a one to one relationship between two classes, but the database schema erroneously maintains the data as a one to many. 我想要做的是在两个类之间创建一对一的关系,但数据库模式错误地将数据维护为一对多。

public class ClassA
{
    public ClassB
    {
        get;
        set;
    }
}

Unfortunately the table ClassB in the database has reference to ClassAId rather than ClassA having a ClassBId as shown here: 不幸的是,数据库中的表ClassB引用了ClassAId而不是具有ClassBId的ClassA,如下所示:

CREATE TABLE [dbo].[ClassA]
    [Id] [bigint] IDENTITY


CREATE TABLE [dbo].[ClassB]
    [Id] [bigint] IDENTITY
    [ClassAId] [bigint]

How to I set up my mapping file that inherits from EntityTypeConfiguration to force this relationship. 如何设置从EntityTypeConfiguration继承的映射文件以强制此关系。

public class ClassAMapping : EntityTypeConfiguration<ClassA>
{
    public ClassA()
    {
        HasKey(f => f.Id);

        // what happens here to force a one to one????
    }
}

Thanks to Jakub Konecki for the link to the article, it did not actually contain the answer I was looking for, but it did link to an earlier post in the series where I found the answer. 感谢Jakub Konecki链接到文章,它实际上并没有包含我正在寻找的答案,但它确实链接到我找到答案的系列中的早期帖子

The way to force this one to one association is as follows: 强制这种一对一关联的方法如下:

public class ClassAMapping : EntityTypeConfiguration<ClassA>
{
    public ClassA()
    {
        HasKey(x => x.Id);

        HasOptional<ClassB>(x => x.ClassB)
                .WithRequired()
                .Map(x => x.MapKey("ClassBId"));
    }
}

This mapping reads as: 此映射读作:

"The ClassA entity has an optional association with one ClassB entity, but this association is required for the ClassB entity."

Please note that this solution is uni-directional and will not allow the following: 请注意,此解决方案是单向的,不允许以下内容:

ClassB b = new ClassB();
string test = b.ClassA.SomeString;

If a bi-directional association is required check out the link that was found which elaborates further. 如果需要双向关联,请查看找到的链接,进一步详细说明。

The article linked by Jakub is part of a series of posts which are a good read if you are trying to sort out your EF4.1 associations. 由Jakub链接的文章是一系列帖子的一部分,如果您正在尝试整理您的EF4.1关联,这些文章是一个很好的阅读。

Take a look here: 看看这里:

http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx

It's an article for CTP5, but I think you will be able to 'translate' fluent API calls to RTm version. 这是一篇针对CTP5的文章,但我认为您可以将流畅的API调用“转换”为RTm版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM