text 首先是EF核心代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 首先是EF核心代码相关的知识,希望对你有一定的参考价值。
1. Create models
public class ShoppingCart
{
public int Id { get; set; }
public int TotalItems {get; set;}
public double TotalPrice {get;set;}
public double OriginalPrice {get;set;}
public int UserId { get; set; }
public User User { get; set; }
public List<ShoppingItem> ShoppingItems { get; set; }
}
public class ShoppingItem
{
public int Id { get; set; }
public int Amount {get;set;}
public Product Product { get; set; }
public Order Order {get;set;}
public int OrderId {get;set;}
public ShoppingCart ShoppingCart { get; set; }
public int ShoppingCartId { get; set; }
}
public class AnnuityTypeDetails
{
public int Id { get; set; }
public string ProgramType { get; set; }
public string SalesType { get; set; }
public string ShortDescription { get; set; }
public string LongDescription { get; set; }
public string Logo { get; set; }
public string MoreInfoLink { get; set; }
public string DetailsView { get; set; }
public string CompanyCode { get; set; }
public bool? IsActive { get; set; }
public DateTime? DateCreated { get; set; }
public bool? IsAnnuityEmailSent { get; set; }
public string EmailLogo { get; set; }
public string DdteamEmail { get; set; }
public string ProgramContactEmail { get; set; }
public string CustomerField1 { get; set; }
public bool? IsTestEmail { get; set; }
public string CmsId { get; set; }
}
2. create context file
public class TrojantradingDbContext : DbContext
{
public TrojantradingDbContext(DbContextOptions options) : base(options)
{
}
public TrojantradingDbContext() { }
public DbSet<User> Users { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<HeadInformation> HeadInformations { get; set; }
public DbSet<ShoppingCart> ShoppingCarts { get; set; }
public DbSet<PdfBoard> PdfBoards { get; set; }
public DbSet<CompanyInfo> CompanyInfos { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<ShippingAddress> ShippingAddresses { get; set; }
public DbSet<BillingAddress> BillingAddresses { get; set; }
public DbSet<ShoppingItem> ShoppingItems { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("trojantrading");
modelBuilder.Entity<AnnuityTypeDetails>(entity =>
{
entity.Property(e => e.CmsId)
.HasMaxLength(30)
.IsUnicode(false);
entity.Property(e => e.CompanyCode)
.HasMaxLength(10)
.IsUnicode(false);
entity.Property(e => e.CustomerField1).HasMaxLength(200);
entity.Property(e => e.DateCreated)
.HasColumnType("datetime")
.HasDefaultValueSql("(getdate())");
entity.Property(e => e.DdteamEmail)
.HasColumnName("DDTeamEmail")
.HasMaxLength(200);
entity.Property(e => e.DetailsView)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.EmailLogo).HasMaxLength(1000);
entity.Property(e => e.IsActive).HasDefaultValueSql("((1))");
entity.Property(e => e.IsAnnuityEmailSent).HasDefaultValueSql("((0))");
entity.Property(e => e.IsTestEmail).HasDefaultValueSql("((1))");
entity.Property(e => e.Logo).IsUnicode(false);
entity.Property(e => e.LongDescription).IsUnicode(false);
entity.Property(e => e.MoreInfoLink).IsUnicode(false);
entity.Property(e => e.ProgramContactEmail).HasMaxLength(60);
entity.Property(e => e.ProgramType)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.SalesType)
.HasMaxLength(3)
.IsUnicode(false);
entity.Property(e => e.ShortDescription)
.HasMaxLength(200)
.IsUnicode(false);
});
modelBuilder.Entity<User>()
.ToTable("user")
.HasKey(u => u.Id);
modelBuilder.Entity<Product>()
.ToTable("product")
.HasKey(p => p.Id);
modelBuilder.Entity<Order>()
.ToTable("order")
.HasKey(o => o.Id);
modelBuilder.Entity<ShoppingCart>()
.ToTable("shoppingCart")
.HasKey(s => s.Id);
modelBuilder.Entity<ShoppingItem>()
.ToTable("shoppingItem")
.HasKey(s => s.Id);
modelBuilder.Entity<PdfBoard>()
.ToTable("pdfBoard")
.HasKey(p => p.Id);
modelBuilder.Entity<HeadInformation>()
.ToTable("headInformation")
.HasKey(h => h.Id);
modelBuilder.Entity<CompanyInfo>()
.ToTable("companyInfo")
.HasKey(c => c.Id);
modelBuilder.Entity<Role>()
.ToTable("role")
.HasKey(r => r.Id);
modelBuilder.Entity<ShippingAddress>()
.ToTable("shippingAddress")
.HasKey(s => s.Id);
modelBuilder.Entity<BillingAddress>()
.ToTable("billingAddress")
.HasKey(s => s.Id);
//user shoppingcart 1:1
modelBuilder.Entity<User>()
.HasOne(u => u.ShoppingCart)
.WithOne(s => s.User)
.HasForeignKey<ShoppingCart>(s => s.UserId);
//user order 1:m
modelBuilder.Entity<Order>()
.HasOne(o => o.User)
.WithMany(u => u.Orders)
.HasForeignKey(o => o.UserId);
//role user 1:m
modelBuilder.Entity<User>()
.HasOne(ur => ur.Role)
.WithMany(r => r.Users)
.HasForeignKey(ur => ur.RoleId);
//order shoppingitem 1:m
modelBuilder.Entity<ShoppingItem>()
.HasOne(s => s.Order)
.WithMany(o => o.ShoppingItems)
.HasForeignKey(s => s.OrderId);
//shopping Cart Shopping Item 1:m
modelBuilder.Entity<ShoppingItem>()
.HasOne(s => s.ShoppingCart)
.WithMany(s => s.ShoppingItems)
.HasForeignKey(s => s.ShoppingCartId)
.OnDelete(DeleteBehavior.Restrict);
//shopping item product 1:1
modelBuilder.Entity<Product>()
.HasOne(p => p.ShoppingItem)
.WithOne(s => s.Product)
.HasForeignKey<Product>(p => p.ShoppingItemId);
// Ship address user 1:m
modelBuilder.Entity<User>()
.HasOne(u => u.ShippingAddress)
.WithMany(s => s.Users)
.HasForeignKey(u => u.ShippingAddressId);
// Bill address user 1:m
modelBuilder.Entity<User>()
.HasOne(u => u.BillingAddress)
.WithMany(b => b.Users)
.HasForeignKey(u => u.BillingAddressId);
//companyInfo user 1:m
modelBuilder.Entity<User>()
.HasOne(u => u.CompanyInfo)
.WithMany(c => c.Users)
.HasForeignKey(u => u.CompanyInfoId);
}
}
3. install nuget package
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
4. reference database in startup.cs
services.AddDbContext<TrojantradingDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
in appsettings.json
"ConnectionStrings": {
"DefaultConnection": "server=OROCHI\\SQLEXPRESS;Database=trojantrading;user id=ddlogin;password=ddlogin;Trusted_Connection=True;"
}
5. run command in console
Add-Migration Trojantrading.Repositories.TrojantradingDbContext
update-database
以上是关于text 首先是EF核心代码的主要内容,如果未能解决你的问题,请参考以下文章