2012-10-11 63 views
1

本周之前我一直没有使用EF(v5),现在我正在做一些数据库更改,它正在做一些非常奇怪的事情。实体框架奇怪的行为

我删除了一个nvarchar国家字段,取而代之的是与一个外键Country表

ALTER TABLE [dbo].[UserProfile] WITH CHECK ADD CONSTRAINT [FK_UserProfile_Countries] FOREIGN KEY([CountryId]) 
REFERENCES [dbo].[Countries] ([Id]) 

更新在VS2012模型的ID列的CountryId INT领域做一些真正unexpecting事情。它报告我删除的字段有错误:

错误11010:关联结束'国家'未映射。

我有两个虚拟域加入到这似乎不正确的模型:

public partial class UserProfile 
{ 
    public int UserId { get; set; } 
    ... 
    public int CountryId { get; set; } 

    public virtual Country Country { get; set; } 
    public virtual Country Country1 { get; set; } 
} 

我读过没有与设计师的错误和手动运行“运行自定义工具” .tt文件修复了这个问题,但似乎没有奏效。

有没有人看过这个或解决了它?

+0

你有现成的数据库还是数据库与项目一起发展?如果后者是真的,你应该考虑使用EF Code First而不是依赖.tt的东西:http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with- entity-framework-4.aspx或http://www.codeguru.com/csharp/article.php/c19233/Introduction-to-Entity-Framework-Code-First.htm –

+0

它随着项目一起发展。这位设计师看起来非常笨重,越野车,SQLMetal的工作就像是一种魅力!它使我完全不用EF。 感谢您的链接,我现在去读这 – Jammer

+0

是的,我从来没有喜欢过设计师。 Code First在EF 4中获得全面支持(我认为是这样),这是一种解脱。没有模型和图表,只是sweeeeeet代码:) –

回答

0

只是为了我通过对数据库这么多时间烧毁记录首先,我抛弃了一切,写代码,而不是...

0

只是为了测试,我从头开始完全重新创建了我的模型。删除所有文件。在从头开始重新创建模型后,在Country.cs文件中查看,恐吓了我的生活...如果我的假设是正确的。该下课这样看:

public partial class Country 
{ 
    public Country() 
    { 
     this.UserProfiles = new HashSet<UserProfile>(); 
    } 

    public int Id { get; set; } 
    public string CountryCode { get; set; } 
    public string CountryName { get; set; } 

    public virtual ICollection<UserProfile> UserProfiles { get; set; } 
} 

如果我对这个假设是出现这看起来可怕的,从性能的角度来看。从看这个班的时候,看起来好像我检索了一下,说美国的那一行,那么它也会加载每一个美国作为国家的档案......或者我会生气吗?设计者今天炒我的大脑......