Question

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,

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top