EF Code First adds extra column to query that doesn't exist in model anymore

让人想犯罪 __ 提交于 2019-12-05 23:41:30

Sorry guys, I knew it should be some small stupid mistake but didn't thought it'd be that stupid. Actually a Link entity had collection of Messages in it. This property wasn't used much so we didn't notice it amongst other properties. And when I removed it - all started to work.

So it started like very interesting question but end up with very obvious answer - human factor.

To all who are new to EF and are falling in the same cave I'll leave a note: EF can't "remember" some properties unless it has reference to it. So if you do experience similar troubles, please double check your code and ask someone else to do it, there is no magic!

I can't upvote now, so soadyp, Gert Arnold - thank you for your will to help!

start here http://msdn.microsoft.com/en-us/data/jj591583.aspx Any chance the

public Link Link { get;set; }

is dragging in Link_Id

If you only have Message and (necessarily Link) in your model there will always be a column Link_Id to accommodate Message.Link.

If you've also got the derived classes off Message in your model the Link_Id that is always generated is for the propertyLinkMessage.Link_Id. You can't have Link in the base class and the foreign key value in a derived class. Now EF just notices that there is a property LinkMessage.Link_Id and besides that a navigation property Link. For the latter, Link_Id is not available any more, so EF created Link_Id1.

You have to put both Link and Link_Id in the same class to make EF see them as two parts of one foreign key (a foreign key association). It could look like this:

public class LinkMessage : Message
{
    [ForeignKey("Link")]
    public int? Link_Id { get; set; }
    public Link Link { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!