locked
How to ignore or stop creating table from class EF code first RRS feed

  • Question

  • User1183902823 posted

    suppose i have a employee class and i want when i will migrate or run our project then EF will not create employee table. so is there any attribute which i can use for a class to ignore it?

    i found NotMapped attribute. does it stop EF to create db table for Product class?

    [NotMapped]
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
    
            public int IssuedQuantity { get; set; }
    
            public int InhandQuantity { get; set; }
    
            public virtual ICollection<OrderedItem> OrderedItems { get; set; }
            ...
        }

    Saturday, December 9, 2017 11:51 PM

Answers

  • User1400794712 posted

    Hi tridip1974,

    NotMapped attribute is applied to properties of an entity class for which we do not want to create a corresponding columns in the database. If you don't want to create table for Product class, you just need to remove this table in DBContext.

    public class TestContext : DbContext
    {
        //Remove following code from Context.
        //public virtual DbSet<Product> Products { get; set; }
    }

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 6:38 AM
  • User753101303 posted

    Hi,

    So my understanding is that you want Product to be part of your db context but don't want EF to migrate or update your db because you already created this table?

    You never enabled EF migration? Also check what you have for https://msdn.microsoft.com/en-us/library/gg679461(v=vs.113).aspx. You could set the strategy to null and then you are entirely on your own to sync your db context and the underlying database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 10:47 AM
  • User1183902823 posted

    you said -- You never enabled EF migration. how to disable EF migration ?

    i know the below code will be working because null has been set to SetInitializer.

    any other way exist say like any annotation which i can use for table to enable / disable migration conditionally?

    public MyContext()
        {
            Database.SetInitializer<MyContext>(null);//Disable initializer
        }

    we can also use it AutomaticMigrationsEnabled = false;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 12:45 PM
  • User753101303 posted

    So you mean you are using EF migrations for some tables but not for all tables ??? Never tried.

    My first thought would be that you have to take care to change your db in two steps ie one step where you'll deal with "hand created" tables and then you'll take care of creating a migration that goes from an initial state to a final state where the only difference is the tables you really want to handle with EF migration.

    Or maybe you could include those tables in an EF migration  using custom code such as shown https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx#Customizing Migrations.

    If not that yet, it seems you are in a quite unusual situation and we would need to understand first this exact situation (so for now my understanding is that parts of your migration uses EF and other parts are created/udpated "by hand") ???

    Or if you ran into some error, it could be easier to directly tell which error you have ?

    Edit: maybe https://github.com/aspnet/EntityFrameworkCore/issues/2725 based on ModelBuilder.Ignore.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 3:39 PM
  • User1400794712 posted

    Hi tridip1974,

    According to your discussing with PatriceSc, I find my understanding of your issue may be not correct at the beginning. Sorry for the inconvenience.

    Do you mean that you are using code first with an existing database and existing table? When you use migration, it will create a new table to database and it will tell you that the table already exists. If so, you can refer to the following steps:

    (1) Using Code First to connect the database.

    (2) Enable-migrations

    (3) Add-migration intial

    Then in the intial.cs, remove all code that duplicates existing tables from inside the Up and Down override methods in the initial migration. But, leave the methods there. Just like:

    public partial class intial : DbMigration
    {
        //Keep the methods and remove the code creating product.
        public override void Up()
        {
            //CreateTable(
            //    "dbo.Product",
            //    c => new
            //        {
            //            Id = c.Int(nullable: false, identity: true),
            //            Name = c.String()
            //        })
            //    .PrimaryKey(t => t.Id);
        }
        public override void Down()
        {
            //DropTable("dbo.Product");
        }
    }

    (4) update-migration

    Then migration will not create the product table. And after it, if you modify the product entity, using migration will also modify the product table in database.

    Here is my reference, you can also refer to it.

    http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database/ 

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 12, 2017 8:34 AM

All replies

  • User1400794712 posted

    Hi tridip1974,

    NotMapped attribute is applied to properties of an entity class for which we do not want to create a corresponding columns in the database. If you don't want to create table for Product class, you just need to remove this table in DBContext.

    public class TestContext : DbContext
    {
        //Remove following code from Context.
        //public virtual DbSet<Product> Products { get; set; }
    }

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 6:38 AM
  • User1183902823 posted

    if i comment this line //public virtual DbSet<Product> Products { get; set; }

    then how could i refer product table in code by like dbcontext.product ?

    Monday, December 11, 2017 9:23 AM
  • User753101303 posted

    Hi,

    So my understanding is that you want Product to be part of your db context but don't want EF to migrate or update your db because you already created this table?

    You never enabled EF migration? Also check what you have for https://msdn.microsoft.com/en-us/library/gg679461(v=vs.113).aspx. You could set the strategy to null and then you are entirely on your own to sync your db context and the underlying database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 10:47 AM
  • User1183902823 posted

    you said -- You never enabled EF migration. how to disable EF migration ?

    i know the below code will be working because null has been set to SetInitializer.

    any other way exist say like any annotation which i can use for table to enable / disable migration conditionally?

    public MyContext()
        {
            Database.SetInitializer<MyContext>(null);//Disable initializer
        }

    we can also use it AutomaticMigrationsEnabled = false;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 12:45 PM
  • User753101303 posted

    So you mean you are using EF migrations for some tables but not for all tables ??? Never tried.

    My first thought would be that you have to take care to change your db in two steps ie one step where you'll deal with "hand created" tables and then you'll take care of creating a migration that goes from an initial state to a final state where the only difference is the tables you really want to handle with EF migration.

    Or maybe you could include those tables in an EF migration  using custom code such as shown https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx#Customizing Migrations.

    If not that yet, it seems you are in a quite unusual situation and we would need to understand first this exact situation (so for now my understanding is that parts of your migration uses EF and other parts are created/udpated "by hand") ???

    Or if you ran into some error, it could be easier to directly tell which error you have ?

    Edit: maybe https://github.com/aspnet/EntityFrameworkCore/issues/2725 based on ModelBuilder.Ignore.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2017 3:39 PM
  • User1400794712 posted

    Hi tridip1974,

    According to your discussing with PatriceSc, I find my understanding of your issue may be not correct at the beginning. Sorry for the inconvenience.

    Do you mean that you are using code first with an existing database and existing table? When you use migration, it will create a new table to database and it will tell you that the table already exists. If so, you can refer to the following steps:

    (1) Using Code First to connect the database.

    (2) Enable-migrations

    (3) Add-migration intial

    Then in the intial.cs, remove all code that duplicates existing tables from inside the Up and Down override methods in the initial migration. But, leave the methods there. Just like:

    public partial class intial : DbMigration
    {
        //Keep the methods and remove the code creating product.
        public override void Up()
        {
            //CreateTable(
            //    "dbo.Product",
            //    c => new
            //        {
            //            Id = c.Int(nullable: false, identity: true),
            //            Name = c.String()
            //        })
            //    .PrimaryKey(t => t.Id);
        }
        public override void Down()
        {
            //DropTable("dbo.Product");
        }
    }

    (4) update-migration

    Then migration will not create the product table. And after it, if you modify the product entity, using migration will also modify the product table in database.

    Here is my reference, you can also refer to it.

    http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database/ 

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 12, 2017 8:34 AM