Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping to existing table using Entity Framework

I face some difficulties when attemp to map my class to existing table using Entity Framework.

My class:

[Table("builder_User")]
public class MobileUser
{
    [Key]
    [Column("id")]
    public int Id { get; set; }
    [Column("beansCount")]
    public int BeansCount { get; set; }
    [Column("bonusSum")]
    public double BonusSum { get; set; }
    [Column("facebookUsername")]
    public string FacebookUserName { get; set; }
    [Column("firstName")]
    public string FirstName { get; set; }
    [Column("lastName")]
    public string LastName { get; set; }
    [Column("guid")]
    public string Guid { get; set; }
    [Column("job")]
    public string Job { get; set; }
    [Column("purchasedSum")]
    public double PurchasedSum { get; set; }
    [Column("facebookId")]
    public string FacebookId { get; set; }

}

My table in the database

enter image description here

And in DataContext class I have:

public DbSet<MobileUser> MobileUsers { get; set; }

But when I try to get users from the database, I get an exception

DbContext has changed since the database was created....

When I run add-migration command, it generates create table command.

So what is my mistake? Thanks

like image 874
Ihor Korotenko Avatar asked Aug 05 '15 09:08

Ihor Korotenko


1 Answers

Set the initializer to null by calling Database.SetInitializer<TContext>(null) or Database.SetInitializer(new NullDatabaseInitializer<TContext>()

By setting the initializer to null, EF will no longer verify the database schema and will no longer create the database if the schema changes. You will have to create/update the database yourself after changing the schema.

like image 65
Dealdiane Avatar answered Oct 20 '22 21:10

Dealdiane