2

I'm having some troubles getting a set of Entities setup to work correctly. I'm using EF v5 in VS2012 against SQL Server 2008 R2 Express.

Everything seems to be correct with the generated database but I'm not having any success writing to some of the tables.

I have a UserProfile object defined like:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    public long UserId { get; set; }

    [StringLength(56)]
    public string UserName { get; set; }

    public Country Country { get; set; }
    ...
}

I also have the Country entity defined like:

[Table("Country")]
public class Country
{
    [Key]
    public int Id { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

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

The generated columns look correct in the database, if I script it out it looks good:

ALTER TABLE [dbo].[UserProfile]  WITH CHECK ADD  CONSTRAINT [UserProfile_Country] FOREIGN KEY([Country_Id])
REFERENCES [dbo].[Country] ([Id])
GO

Just for testing purposes in a controller action I have the following:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new 
{
    Country = new Country { Id = 223, CountryCode = "UK", CountryName = "UK" },
    ...
});

When this WebSecurity method is executed I get the following error:

No mapping exists from object type MyApi.Data.Entities.Country to a known managed provider native type.

I tried setting up a configuration file to specify the relationship in code as well as the database but it's still not playing friendly.

The config looks like:

public class CountryEntityConfig : EntityTypeConfiguration<Country>
{
    public CountryEntityConfig()
    {
        this.HasMany(x => x.UserProfiles)
            .WithRequired()
            .HasForeignKey(FKey => FKey.Country);            
    }
}

It feels really odd having a list of profiles in the country object am I getting some of the basics completely wrong with this? Any input appreciated.

EDIT:

I've revisted some classes and made a few changes, in the user profile I now have:

    public int CountryId { get; set; }

    public virtual Country Country { get; set; }

Country.cs

public class Country
{
    public int CountryId { get; set; }

    [StringLength(2)]
    public string CountryCode {  get; set; }

    [StringLength(100)]
    public string CountryName { get; set; }

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

And the database now contains two CountryId related fields in the UserProfile table with relationships to the Country table and I still get the original error message.

TIA,

Jammer
  • 9,969
  • 11
  • 68
  • 115

1 Answers1

1

The No mapping error is probably because you did not add the Country entity to the DbContext.

In the AccountModels.cs file, you will find a UsersContext that derives from DbContext, in there you must add a DbSet<Country>.

There are, however, other issues here. For instance, you are creating your Country table with a Key, and EF by default will give the Country table an autogenerated identity column. This means you can't insert a value for the ID, it will be auto generated.

Country is probably a lookup table anyways that you will populate with country values. So you would probably just set CountryId to the value of the country you want

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 1
    The Country entity is in the DbContext already. The examples of this I've seen always use an instance of the class rather than an Id property. Do you mean that I need to include an `int CountryId` property in the UserProfile class and let EF work it out? – Jammer Oct 12 '12 at 22:36
  • @Jammer - you're telling EF that you want to insert a new country, because you're creating a new country object. While you can do that, it's probably not what you want. And if you do, you can't supply an ID because the ID is auto-generated. If you just want to create a user and assign an id, then you should assign it via the CountryID property of the user. – Erik Funkenbusch Oct 12 '12 at 23:19
  • @Jammer - it was clear from your description that you had added the Country table to the DbContext. – Erik Funkenbusch Oct 12 '12 at 23:21
  • After making the changes I have two fields in the UserProfile table one called CountrId and another called Country_CountryId both with foreign keys to the Country table ... I'm getting more confused!! My classes are setup exactly like this example shows http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx – Jammer Oct 12 '12 at 23:50