代码优先迁移:引用实体框架核心

时间:2016-07-07 15:15:43

标签: c# asp.net-mvc entity-framework entity-framework-core

我在ASP.NET(Core 1.0)MVC框架中有以下模型。我正在使用Entity Framework Core。我想创建一个表" Deliveries"使用同一个表中的两个外键地址(一个接收地址和一个下拉地址)。我的实施是错误的,所以我想问你是否知道:

  1. 如何声明这些属性
  2. 如何为这些键设置onupdate和ondelete操作?
  3. ApplicationDbContext.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using JBCoreApp.Models;
    using Microsoft.EntityFrameworkCore.Metadata;
    
    namespace JBCoreApp.Data
    {
        public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }
    
            public DbSet<Country> Countries { get; set; }
            public DbSet<AddressType> AddressTypes { get; set; }
            public DbSet<Address> Addresses { get; set; }
            public DbSet<Profile> Profiles { get; set; }
            public DbSet<VehicleType> VehicleTypes { get; set; }
            public DbSet<LicenseCategory> LicenseCategories { get; set; }
            public DbSet<Driver> Drivers { get; set; }
            public DbSet<DriverLicense> DriverLicenses { get; set; }
            public DbSet<Priority> Priorities { get; set; }
            public DbSet<Sender> Senders { get; set; }
            public DbSet<Status> Statuses { get; set; }
            public DbSet<Vehicle> Vehicles { get; set; }
            public DbSet<Delivery> Deliveries { get; set; }
            public DbSet<DeliveryStatus> DeliveryStatus { get; set; }
    
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(builder);
                // Customize the ASP.NET Identity model and override the defaults if needed.
                // For example, you can rename the ASP.NET Identity table names and more.
                // Add your customizations after calling base.OnModelCreating(builder);
    
                builder.Entity<Delivery>()
                    .HasOne(a => a.DropOffAddress)
                    .WithOne()
                    .HasForeignKey<Address>(a => a.Id)
                    .OnDelete(DeleteBehavior.Restrict);
    
    
                builder.Entity<Delivery>()
                    .HasOne(a => a.PickUpAddress)
                    .WithOne()
                    .HasForeignKey<Address>(a=> a.Id)
                    .OnDelete(DeleteBehavior.Restrict);
    
    
            }
        } 
    }
    

    Address.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace JBCoreApp.Models
    {
        [Table("Addresses")]
        public class Address
        {
            public Address()
            {
    
            }
            [Key]
            public int Id { get; set; }
    
            public int ProfileId { get; set; }
            public Profile Profile { get; set; }
    
            public int TypeId { get; set; }
            [ForeignKey("TypeId")]
            public AddressType AddressType { get; set; }
    
            public string Street { get; set; }
            public string StreetNumber { get; set; }
            public string PostCode { get; set; }
            public string City { get; set; }
    
            public string CountryId { get; set; }
            public Country Country { get; set; }
    
            public string Notes { get; set; }
        }
    }
    

    Delivery.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace JBCoreApp.Models
    {
        [Table("Deliveries")]
        public class Delivery
        {
            public Delivery()
            {
    
            }
    
            [Key]
            public int Id { get; set; }
    
            [Required]
            public int SenderId { get; set; }
            public Sender Sender { get; set; }
    
            [Required]
            public int VehicleTypeId { get; set; }
            public VehicleType VehicleType { get; set; }
    
            [Required]
            public int PriorityId { get; set; }
            public Priority Priority { get; set; }
    
            public int DriverId { get; set; }
            public Driver Driver { get; set; }
    
            [ForeignKey("PickUpAddressId")]
            public Address PickUpAddress { get; set; }
            public int PickUpAddressId { get; set; }
    
            [ForeignKey("DropOffAddressId")]
            public Address DropOffAddress { get; set; }
            public int DropOffAddressId { get; set; }
    
            public DateTime PickUpDateTime { get; set; }
            public DateTime DropOffDateTime { get; set; }
            public DateTime Deadline { get; set; }
    
            public string Description { get; set; }
            public string Price { get; set; }
    
            [Timestamp]
            public Byte[] TimeStamp { get; set; }
        }
    }
    

0 个答案:

没有答案
相关问题