entity framework code first how to use it with an existing database

1

Good, I have a database that was not created with Entity Framework code first. It has tables and independent data, then, create another independent web project in which if I use EF. It is possible to use the BD that already exists to put the EF entities there. That is to say that when I do the update-database, DO NOT touch the other tables and relationships?

I make a clarification. I want the entity Framework NOT TO SEE THE TABLES I have already created, I just want to use the same instance of BD

    
asked by NioDeTark 27.09.2017 в 16:32
source

2 answers

1

If you can use it, just map the classes with the tables and add them to the model. But if you are already using C # migrations to generate the tables in sql. You're going to get in trouble. Because you will try to create tables that are already in the BD.

    
answered by 27.09.2017 в 17:02
0

If the database was already created, I would recommend using Database First to generate the entities and the mapping automatically.

Here you can find some documentation of Database First: link

On the other hand, if you want to use Code First, you would have to map each of the tables.

With regard to mapping, this is manual and requires too much effort since you would have to do table by table.

Below is an example of mapping the user table.

public class UsuarioMapping : EntityTypeConfiguration<Usuario>
    {
        public UsuarioMapping()
        {
            HasKey(x => x.Id);
            Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("id");
            Property(x => x.Nombre).HasColumnName("nombre");
            Property(x => x.ApellidoPaterno).HasColumnName("apellidoPaterno");
            Property(x => x.ApellidoMaterno).HasColumnName("apellidoMaterno");
            Property(x => x.Correo).HasColumnName("correo");
            Property(x => x.NombreUsuario).HasColumnName("usuario");
            Property(x => x.Contrasena).HasColumnName("contrasena");
            Property(x => x.Estatus).HasColumnName("estatus");
            ToTable("tblUsuarios");
        }
    }

The context would be as follows:

public class MainContext : DbContext
    {

        public DbSet<Usuario> Usuario { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            try
            {                
                var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                    .Where(type => !String.IsNullOrEmpty(type.Namespace))
                    .Where(type => type.BaseType != null && type.BaseType.IsGenericType
                    && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
                foreach (var type in typesToRegister)
                {                    
                    dynamic configurationInstancie = Activator.CreateInstance(type);                    
                    modelBuilder.Configurations.Add(configurationInstancie);                    
                }

                base.OnModelCreating(modelBuilder);
            }
            catch (Exception e)
            {
                //handle
            }
        }       
    }
    
answered by 27.09.2017 в 23:33