Entity Framework Inserts the entity Already Exists in Database

asp.net-mvc c# entity-framework entity-framework-6

Question

I'm using Identity and EF 6 Code-First MVC.

When I attempt to register a new user via my website, EF tries to insert an object that already exists in the database even though I have changed the Identity framework's standard ApplicationUser class.

//some code above here

using (ApplicationDbContext db = new ApplicationDbContext())
{
    // I load the City object form db based on the model passed to current method
    City city = db.Countries.Include("States.Cities")
        .First(c => c.Code == model.CountryCode)
        .States.First(s => s.Id == model.StateId)
        .Cities.First(ci => ci.Name == model.CityName);

    // I create a new Employee and a new Company and Company has an Address 
    // referring to the City I loaded from db
    Employee employee = new Employee
    {
        FirstName = model.FirstName,
        LastName = model.LastName,
        Company = new Company
        {
            Name = model.CompanyName,
            OfficePhone = model.OfficePhone,
            Address = new Address { City = city, Street = model.Street, ZipCode = model.ZipCode }
        }
    };

    // This is part of Identity framework code
    var user = new ApplicationUser
    {
        UserName = model.Email,
        Email = model.Email,
        // my custom code: I assign employee to the standard ApplicationUser class
        UserProfile = employee
    };

    // This is going to save the new user to database, 
    // but it tries to insert a new Country object into db. 
    // Note that City class has a property of type State 
    // and State class has a property type of Country 
    var result = await UserManager.CreateAsync(user, model.Password);

    // more code below

It seems from the error I get that EF is adding the Country object to the database. I suppose that occurs when EF adds Address to the database. While City, State, and Country are already present in my code, Address is a new object, and you can see that I'm first fetching City from a database. I attempted to use the code below, but it was unsuccessful:

db.Entry(employee.Company.Address.City).State = EntityState.Unchanged;

I apologise for the extensive code, but I did my best to shorten it.

This is how I'd act:

public class Country
{
    string Code;
    string Name;
    List<State> States;
}

public class State
{
    int Id;
    string Name;
    List<City> Cities;
}

public class City
{
    int Id;
    string Name;
    State State;
}

public class Address
{
    string Street;
    string ZipCode;
    City City;
}

public class Company
{
    string Name;
    string OfficePhone;
    Address Address;
}
1
0
12/13/2017 5:55:50 PM

Accepted Answer

An old issue of mine was like that. I don't sure what the issue is, but I have a solution:

change the code:

var result = await UserManager.CreateAsync(user, model.Password);

the following code:

var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var result = UserManager.Create(user, model.Password);

This ensures accurate data saving.

0
12/15/2017 5:32:30 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow