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