tme*_*ser 7 c# database entity-framework
我们正在尝试使用现有数据库在我们的商店中使用Entity框架(因此,更改数据库模式不是一个选项),并且我们为测试事物创建的单元测试显示了一些非常奇怪的行为.
这是它为我们拥有的特定对象吐出的SQL:
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]
Run Code Online (Sandbox Code Playgroud)
正如您可能注意到的那样,请求的最后两列与其他列不同.那是因为它们实际上并不存在,我们不知道实体为什么要求它们!我们的配置文件和我们的POCO都没有提及它们.事实上,就我们的数据库而言,它们是完全独立的概念,并不是直接相关的.
从哪里获取这些列,以及如何告诉它将其删除?
编辑:回答下面的一些问题,1)我们正在使用实体框架4.2.我们正在使用流畅的映射.
2)POCO本身看起来像是这样,为了简洁起见,切除了平等的混乱:
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; }
Run Code Online (Sandbox Code Playgroud)
3)我们没有使用edmx.我们有一个自定义DbContext.没有太多的线条非常有趣.这两个可能是有趣的:
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Run Code Online (Sandbox Code Playgroud)
除此之外,Context文件很多
modelBuilder.Configurations.Add(new WhateverConfiguration())
Run Code Online (Sandbox Code Playgroud)
和
public IDbSet<WhateverPoco> PocoDatabaseTableAccessor { get; set; }
Run Code Online (Sandbox Code Playgroud)
4)我们从db-first开始,但是没有用,所以我们目前正在进行代码优先.
5)这是特定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");
Run Code Online (Sandbox Code Playgroud)
问题不在于您显示的映射或类。检查你的Album和StatusMessage班级。它们是实体吗?它们被映射了吗?他们有评论的集合导航属性吗?如果是,EF 期望Comment这些表必须具有 FK。如果表没有此类列,则无法将这些导航属性映射到这些实体中。
顺便提一句。Comments表中的 id 不应该CommentId代替吗DataPartId?