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

sql:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
[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);
 
 
    }

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//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 @   ®Geovin Du Dream Park™  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2018-02-05 Sybase SQL anywhere5.5
2015-02-05 csharp: SQL Server 2005 Database Backup and Restore using C#
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示