Reputation: 141
Models: GropsAndProducts
, Groups
, Products
:
public class GropsAndProducts
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[ForeignKey("Products")]
public int ProductId { get; set; }
[ForeignKey("Groups")]
public int GroupId { get; set; }
public Groups Groups { get; set; }
public Products Products { get; set; }
}
public class Groups
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Discription { get; set; }
public List<GropsAndProducts> groupAndProducts { get; set; }
}
public class Products
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Unit { get; set; }
[Required]
public int Count { get; set; }
[Required]
public int Price { get; set; }
[Required]
public int Discount { get; set; }
public string Discription { get; set; }
public List<GropsAndProducts> groupAndProducts { get; set; }
}
Context:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MySite.Models;
using System.Data;
namespace MySite.Data
{
public class DbProductsAndGroupsContext : DbContext
{
public DbProductsAndGroupsContext(DbContextOptions<DbProductsAndGroupsContext> option)
: base (option)
{
}
public DbSet<Products> products;
public DbSet<Groups> groups;
public DbSet<GropsAndProducts> gropsAndProducts;
}
}
Startup configuration:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DbProductsAndGroupsContext>(option =>
option.UseMySql(
Configuration.GetConnectionString("DefaultConnection"), x=> x.MigrationsHistoryTable("__MyMigrationsHistory", "mySchema"))
);
services.AddDbContext<DbUserContex>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<Users>()
.AddEntityFrameworkStores<DbUserContex>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Now the problem is:
The first migration (From UserIdentity
) was performed normally (that is, a new database was created as well as tables with records). But after I created the second context and try to execute the command (in the package manager) ʻEntityFrameworkCore \ Add-Migration -name: asdf -context: DbProductsAndGroupsContext`, then ** blank ** migration is added. Why can it be like that ??? I will be glad to any information.
Upvotes: 0
Views: 103
Reputation: 5459
Took me a bit to spot your issue. Your DbSet
s need to have getters and setters for EntityFramework to be able to locate the models so that it can generate the needed migration.
You should change:
public DbSet<Products> products;
public DbSet<Groups> groups;
public DbSet<GropsAndProducts> gropsAndProducts;
To:
public DbSet<Products> products { get; set; }
public DbSet<Groups> groups { get; set; }
public DbSet<GropsAndProducts> gropsAndProducts { get; set; }
Note that by convention, DbSet
property names are normally upper cased (i.e: Products
instead of products
). Another thing to point out is you can simplify your GropsAndProducts
(please fix the spelling!) model by changing it to:
public class GropsAndProducts
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public int ProductId { get; set; }
public int GroupId { get; set; }
public Groups Group { get; set; }
public Products Product { get; set; }
}
The foreign key annotation isn't necessary if you follow EF naming conventions (The Group
navigation property's foreign key is GroupId
). The property name Groups
probably doesn't make as much sense anyway since the property is only holding a single Group
Upvotes: 3