CSharp:donet 7 Many to Many Relationship with EF Core 7.02

sql:

IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].GeovinDuMap') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE GeovinDuMap 
GO
CREATE TABLE  GeovinDuMap
(
	Id int  IDENTITY(1,1) primary key NOT NULL, 
	ManAreaName nvarchar(50)
)
go

select * from GeovinDuMap
go



IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].GeovinDuYear') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE GeovinDuYear 
GO
CREATE TABLE  GeovinDuYear
(
	Id int  IDENTITY(1,1) primary key NOT NULL, 
    YearAutoName nvarchar(50)

)
go

select * from GeovinDuYear
go


IF EXISTS (select * from sysobjects where id = object_id(N'[dbo].GeovinDuList') and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
DROP TABLE GeovinDuList 
GO
CREATE TABLE  GeovinDuList
(

	Id int  IDENTITY(1,1) primary key NOT NULL, 
	TookKitId int
		Foreign Key REFERENCES GeovinDuMap(Id),
	YearId int
		Foreign Key REFERENCES GeovinDuYear(Id),
	SingName nvarchar(50)
)
go


select * from GeovinDuList
go

  

 

 

[Table("GeovinDuMap")]
public  class GeovinDuMap
{

    public GeovinDuMap()
    { }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <param name="manAreaName"></param>
    public GeovinDuMap(int id, string manAreaName)
    {

        Id = id;
        ManAreaName = manAreaName;
    }

    [Key]
    public int Id { get; set; }

    [StringLength(50)]
    public string ManAreaName { get; set; }

    public virtual ICollection<GeovinDuList> GeovinDuList { get; set; }
}
//Cross Table
[Table("GeovinDuList")]
public class GeovinDuList
{
    [Key]
    public int Id { get; set; }
    public int TookKitId { get; set; }
    public int YearId { get; set; }
    /// <summary>
    /// 
    /// </summary>
    [StringLength(50)]
    public string SingName { get; set; } = "";

    [ForeignKey("TookKitId")]
    public virtual GeovinDuMap GeovinDuMap { get; set; }

    [ForeignKey("YearId")]
    public virtual GeovinDuYear GeovinDuYear { get; set; }
}


[Table("GeovinDuYear")]
public class GeovinDuYear
{
    [Key]
    public int Id { get; set; }
    [StringLength(50)]
    public string YearAutoName { get; set; }

    public virtual ICollection<GeovinDuList> GeovinDuList { get; set; }
}
#endregion

public class DuDbContext : DbContext
{
    public DbSet<GeovinDuMap> GeovinDuMap { get; set; }
    public DbSet<GeovinDuYear> GeovinDuYear { get; set; }

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


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=geovindu\\GEOVIN2008;Database=EntityFrameworkgeovindu;User ID=sa;Password=geovindu");
        //.ReplaceService<IModelCacheKeyFactory, DynamicModelCacheKeyFactory>();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="modelBuilder"></param>
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //modelBuilder.Entity<GeovinDuMap>(
        //         dob =>
        //    {
        //        dob.ToTable("GeovinDuMap");
        //        dob.Property(o => o.Id).HasColumnName("Id");
        //        dob.Property(o => o.ManAreaName).HasColumnName("ManAreaName");
        //    });

        //modelBuilder.Entity<GeovinDuList>(
        // dob =>
        // {
        //     dob.ToTable("GeovinDuList");
        //     dob.Property(o => o.Id).HasColumnName("Id");
        //     dob.Property(o => o.TookKitId).HasColumnName("TookKitId");
        //     dob.Property(o => o.SingName).HasColumnName("SingName");
        // });

        //modelBuilder.Entity<GeovinDuYear>(
        // dob =>
        // {
        //     dob.ToTable("GeovinDuYear");
        //     dob.Property(o => o.Id).HasColumnName("Id");
        //     dob.Property(o => o.YearAutoName).HasColumnName("YearAutoName");
        // });


        //modelBuilder.Entity<GeovinDuList>()
        //    .HasKey(ky => new { ky.TookKitId, ky.YearId });

        modelBuilder.Entity<GeovinDuList>()
            .HasKey("Id");

        modelBuilder.Entity<GeovinDuList>()
            .HasOne(ky => ky.GeovinDuMap)   //
            .WithMany(k => k.GeovinDuList) //多对多
            .HasForeignKey(ky => ky.TookKitId);  

        modelBuilder.Entity<GeovinDuList>()
            .HasOne(ky => ky.GeovinDuYear)  //
            .WithMany(y => y.GeovinDuList)  //多对多
            .HasForeignKey(ky => ky.YearId);

        base.OnModelCreating(modelBuilder);


    }

  

调用:

//Console.Read();
//初始化表格至数据库
//DuDbContext context = new();
//await context.Database.MigrateAsync();


DuDbContext duDbContext = new DuDbContext();



//GeovinDuMap map= new GeovinDuMap();
//map.ManAreaName = "涂聚文";
//duDbContext.GeovinDuMap.Add(map);
//int k=duDbContext.SaveChanges();
//if(k>0)
//{
//    Console.WriteLine("ok");

//}
//else
//{
//    Console.WriteLine("no");
//}


var dulist = duDbContext.GeovinDuMap.ToList();//duDbContext.GeovinDuMap.Select(d => d.Id).ToList();  
foreach (var d in dulist)
{
    Console.WriteLine(d.ManAreaName);
}

//GeovinDuYear year= new GeovinDuYear();
//year.YearAutoName = "y2010";
//duDbContext.GeovinDuYear.Add(year);
//duDbContext.SaveChanges();
var duyear=duDbContext.GeovinDuYear.ToList();
foreach(var d in duyear)
{
    Console.WriteLine(d.YearAutoName);
}

GeovinDuList geovinDuList=new GeovinDuList();
//geovinDuList.Id= 1;
geovinDuList.TookKitId = 2;
geovinDuList.YearId = 3;
geovinDuList.SingName = "涂聚文";
duDbContext.GeovinDuList.Add(geovinDuList); 
int dok=duDbContext.SaveChanges();
if (dok > 0)
{
    Console.WriteLine("ok,GeovinDuList");

}
else
{
    Console.WriteLine("no,GeovinDuList");
}

 

输出:

 

 

 

 

 

posted @ 2023-02-05 23:07  ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报