user3206357
user3206357

Reputation: 413

Adding migration for a new table

I have created model named "Allocation" but when I am running Add-Migration "Allocation for Deals" then the migration file which is opened with up() and down() functions has no any query within the functions and table is not created in database.

My Model is:-

 class Allocation
    {
        [Display(Name = "ID", ResourceType = typeof(Resources.Resource))]
        public int AllocationId { get; set;}

        [Display(Name = "CountryID", ResourceType = typeof(Resources.Resource))]
        public int CountryId { get; set; }

        [Display(Name = "RightID", ResourceType = typeof(Resources.Resource))]
        public int RightId { get; set; }

        [Display(Name = "Amount", ResourceType = typeof(Resources.Resource))]
        public double Amount { get; set; }
    }

Upvotes: 0

Views: 894

Answers (2)

user3206357
user3206357

Reputation: 413

While adding any migration for new table then make sure that the model class should also be added in DbContext. For ex. For Allocation Model I have added

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

in DbContext but then I was getting "Inconsistent accessibility" error then I just removed "public" keyword from DbSet code.Now the code becomes

DbSet<Allocation> Allocation { get; set; }

Now it works properly.

Upvotes: 1

Ofiris
Ofiris

Reputation: 6151

Did you add a DbContext ? DbSet ?

public class AllocationContext : DbContext
{
    public DbSet<Allocation> Allocation { get; set; }
}

EF needs to know which entities to generate.

DbContext figures out what classes to include in the model by looking at the DbSet properties that are defined.

You can add the DbSet line to your existing (if you already have one) DbContext.

Also note, you should add the [Key] Data Annotation above AllocationId:

[Key]
[Display(Name = "ID", ResourceType = typeof(Resources.Resource))]
public int AllocationId { get; set;}

EDIT: Thanks Colin for the comment, the you don't have to add [Key] annotation as the AllocationIdis in the correct Id convention which will be processed as key.

Upvotes: 3

Related Questions