简体   繁体   中英

Entity Framework Odd Behaviour

I've not been working with EF until this week (v5) and now I'm making a few database changes it's doing some really weird things.

I deleted an NVARCHAR Country field and replaced that with an CountryId int field that is a Foreign Key to the Id column of the Country table

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

Updating the model in VS2012 is doing some really unexpecting things. Its reporting an error on the field I removed:

Error 11010: Association End 'Country' is not mapped.

And I have two virtual fields added to the model which doesn't seem correct:

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; }
}

I've read there is a bug with the designer and manually running "Run Custom Tool" on the .tt files fixes this but it doesn't appear to have worked.

Has anyone seen this or solved it?

Just for testing I've totally recreated my model from scratch. Deleted all the files. After recreating the model from the ground up looking in the Country.cs file scared the life out of me ... if my assumption is correct. The class ended up looking like this:

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; }
}

If my assumption about this is as it appears this looks terrifying from a performance point of view. From looking at that class it looks like if I retrieved say, the row for the United States it would then also load every profile with US as the country ... or am I going mad? The designer has fried my brain today ...

只是为了记录,我在Database First上花了很多时间,所以我放弃了所有这些,而是​​编写代码...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM