Entity Framework Model and Foreign Key Property

I have 2 tables that I import into an EF model.
The first table has the [section] property, which acts as a foreign key to the second table. When I map this property in the model to the table and try to compile, I get this error:

The problem with displaying fragments on lines 158, 174: Non-primary key columns (sections) [Section] are displayed in both fragments to different conceptual side properties - this mismatch is possible because the corresponding conceptual side of the property can be independently changed.

If I remove this property from the model that it passes, but when I request data, I do not have a section field.

I know that I can get it using the navigation field and reading this property from the second table, but for it to work, I have to include another table in my query.

var res  = from name in Context.Table1.Include("Table2")...

Why do I need to enable association for only one field?

UPDATE
To make this clearer:

Table 1 has the fields:
ItemId -
section key - foreign key
name

Table 2 has the fields:
SectionId - key
Name

When I establish associations, the section property from the first table should be removed.

+3
source share
2 answers

In EF 4, you can use FK associations for this.

EF 1 :

var q = from t1 in Context.Table1
        where //...
        select new 
        {
            T1 = t1,
            Section = t1.Section.SectionId
        };
var section = q.First().Section;

, EntityKey:

var t1 = GetT1();
var section = (int)t1.SectionReference.EntityKey.Values[0].Value;

, . EF , MergeOption NoTracking.

0

? , - .

.. , EDMX, . , , , SQL, .

+1

Source: https://habr.com/ru/post/1743688/


All Articles