Entity Framework: Where the hell does it get these columns?

We are trying to get the Entity infrastructure to work in our store with an existing database (and, therefore, changing the database schema is NOT an option), and the unit tests that we created for testing show really strange behavior.

This is the SQL that it spills out for a specific object that we have:

SELECT [Extent1].[CommentTypeId] AS [CommentTypeId], [Extent1].[DataPartId] AS [DataPartId], [Extent1].[CommentId] AS [CommentId], [Extent1].[CreatedTime] AS [CreatedTime], [Extent1].[Message] AS [Message], [Extent1].[From] AS [From], [Extent1].[Likes] AS [Likes], [Extent1].[SourceTypeId] AS [SourceTypeId], [Extent1].[StatusMessage_DataPartId] AS [StatusMessage_DataPartId], [Extent1].[Album_DataPartId] AS [Album_DataPartId] FROM [dbo].[Comments] AS [Extent1] 

The last two requested columns, as you may have noticed, are not like the rest. This is because they do not really exist, and we do not know why Entity asks them! Neither our configuration files nor our POCOs mention them at all. In fact, as far as our database is concerned, they are completely separate and not directly related.

Where do these columns come from, and how can I tell it to cut out?

EDIT: To answer some of the questions below, 1) We use Entity Framework 4.2. We use a smooth display.

2) POCO itself looks like this: with reduced clutter for brevity:

 public long DataPartId { get; set; } public string CommentId { get; set; } public DateTime? CreatedTime { get; set; } public string Message { get; set; } public string From { get; set; } public int? Likes { get; set; } public string SourceTypeId { get; set; } public int CommentTypeId { get; set; } public virtual DataPart DataPart { get; set; } public virtual CommentType CommentType { get; set; } 

3) We do not use edmx. We have a custom DbContext. Not too many lines that are terribly interesting. These two are probably of interest:

  Configuration.LazyLoadingEnabled = true; Configuration.ProxyCreationEnabled = true; 

In addition, there are many context files

 modelBuilder.Configurations.Add(new WhateverConfiguration()) 

and

 public IDbSet<WhateverPoco> PocoDatabaseTableAccessor { get; set; } 

4) First we started with db-first, but that didn’t work, so we are doing the code first.

5) This is the courage of the configuration for this particular POCO:

  HasRequired (x => x.DataPart) .WithRequiredDependent (x => x.Comment); HasRequired (x => x.CommentType) .WithMany (x => x.Comments) .HasForeignKey (x => x.CommentTypeId); HasKey (x => x.DataPartId); ToTable ("Comments", "dbo"); 
+7
source share
3 answers

The problem is not with the display or the class you showed. Check out the Album and StatusMessage . Are they entities? Are they displayed? Do navigation properties have comments for navigation? If so, then EF expects Comment to have an FK for these tables. If the table does not have such a column, you cannot have these navigation properties displayed in these objects.

Btw. Should the identifier in the Comments table be CommentId instead of DataPartId ?

+4
source

Entity Framework, like MVC, uses many conditional configurations. This means that he accepts certain things if you do not say so.

However, it is really strange here, based on the information you provide. According to the SQL query, this comes from the comment table, however your free display indicates that DataPartId is the primary key. Do you have additional primary key smooth mappings? If not, your mappings may be incorrect. Have you checked the actual database to see if the data model matches what you are trying to do?

I assume that your StatusMessage and Album classes have navigation properties for comments, but since you defined DataPartId as the primary key, this is the value that it uses to search for comments, not CommentId.

+1
source

Open .edmx in an XML editor and search for these columns. They should be somewhere in your model.

EDIT: your original question did not mention that you are using the code in the first place. I wonder what your problem was with the database, which usually works well. First, a code or model first, you usually create a database after creating the model (using the generated SQL scripts).

You declared the last two properties virtual, so the generated SQL looks different. From the code that you show us, we can’t see where the link to the album comes from.

Since you have a database, I would generate .edmx from the model in one project. You can then use the POCO code generator or the Self-tracking entity generator to generate objects and store them in another project. Or you can write them manually, as you already know. Property names must match the columns in the database.

0
source

All Articles