本文介绍了遇到严重问题,实体框架和放大器;外键:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我的EDMX以下实体: -

I've got the following entities on my EDMX :-

这两个entites的被产生的更新模型从数据库

These two entites were generated by Update Model From Database.

现在,请注意我的国家怎么有以下主键: -

Now, notice how my country has the following primary key :-

命名&安培; ISO code

这是因为每个国家都在系统中的名称和异code独一无二的。

this is because each country is UNIQUE in the system by Name and IsoCode.

现在,用我的国...这是类似的。主键是: -

Now, with my States ... it's similar. Primary Key is :-

命名&安培; CountryId

每个状态是通过名称和每个国家唯一的。

Each state is unique by name and per country.

现在,各国的外键是一个CountryId。这是SQL: -

Now, the Foreign Key for States is a CountryId. This is the sql :-

ALTER TABLE [dbo].[States]  WITH CHECK ADD
        CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryId])
REFERENCES [dbo].[Countries] ([CountryId])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]
GO

pretty简单的东西。

Pretty simple stuff.

但的EntityFramework不喜欢:(这是假设,我需要一些属性从国家实体在这两个主键的属性连接国家实体。

BUT EntityFramework doesn't like it :( It's assuming that i need to connect some properties from State entity to both primary key properties in the Country entity.

是否有可能增加国家和国家之间的关联上Country.CountryId<? - > State.CountryId ......就像我在我的数据库已映射

干杯;)

推荐答案

在EF(3.5和4.0)FKS必须指向主键。

In EF (3.5 and 4.0) FKs MUST point to Primary Keys.

但你似乎试图指向一个候选键(即[国家]。​​[CountryId]

But you appear to be attempting to point to a Candidate Key (i.e. [Countries].[CountryId]

我知道,这是后话了EF团队正在考虑,虽然在接下来的版本:)

I know that this is something the EF team are considering for the next version though :)

希望这有助于

亚历

这篇关于遇到严重问题,实体框架和放大器;外键:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:23