使用 CodeFirst 自定义多对多映射

发布于 2024-12-08 14:23:13 字数 1478 浏览 0 评论 0原文

开始在我的 MVC 项目中使用 CodeFirst 并遇到一些麻烦。 我有正在使用的数据库和一些预定义的模式。 有一些表:

[Persons]
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](30) NOT NULL,
[Birthday] [datetime] NOT NULL,
[Address] [nvarchar](30) NOT NULL,
[Zip] [nvarchar](5) NOT NULL,
[City] [nvarchar](30) NOT NULL,
[Tax] [money] NOT NULL,
[Memo] [varbinary](max) NULL

[Seminars]
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](20) NOT NULL

和多对多连接表

[QualRef]
[SemID] [bigint] NOT NULL,
[RefID] [bigint] NOT NULL

,其中 SemID 引用 Seminars.ID 和 RefID 引用 Persons.ID

我正在尝试使用配置类修复我的 CodeFirst 架构绑定:

class PersonConfiguration : EntityTypeConfiguration<Person>
    {
        internal PersonConfiguration()
        {
            this.HasMany(i => i.Seminars)
                .WithMany(c => c.Persons)
                .Map(mc =>
                {
                    mc.MapLeftKey("SemID");
                    mc.MapRightKey("RefID");
                    mc.ToTable("QualRef");
                });
        }
    }

并使用以下代码注册它:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().ToTable("Persons");
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new PersonConfiguration());
    }

但是当我使用此模型运行应用程序时,发生了错误 - CodeFirst 试图找到某个表“dbo.People”(?!),但它不存在(预期)。感谢您的好回答!

Start using CodeFirst in my MVC projects and have some trouble.
I have in-use database with some predefined schema.
There are some tables:

[Persons]
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](30) NOT NULL,
[Birthday] [datetime] NOT NULL,
[Address] [nvarchar](30) NOT NULL,
[Zip] [nvarchar](5) NOT NULL,
[City] [nvarchar](30) NOT NULL,
[Tax] [money] NOT NULL,
[Memo] [varbinary](max) NULL

[Seminars]
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](20) NOT NULL

and many-to-many connected table

[QualRef]
[SemID] [bigint] NOT NULL,
[RefID] [bigint] NOT NULL

where SemID referenced to Seminars.ID and RefID referenced to Persons.ID

I'm trying to fix-up my CodeFirst schema binding using configuration class:

class PersonConfiguration : EntityTypeConfiguration<Person>
    {
        internal PersonConfiguration()
        {
            this.HasMany(i => i.Seminars)
                .WithMany(c => c.Persons)
                .Map(mc =>
                {
                    mc.MapLeftKey("SemID");
                    mc.MapRightKey("RefID");
                    mc.ToTable("QualRef");
                });
        }
    }

and register it with this code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().ToTable("Persons");
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new PersonConfiguration());
    }

But when I run app using this models error occured - CodeFirst trying to find some table "dbo.People" (?!) and it does not exist (expected). Thank for good answers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2024-12-15 14:23:13

尝试像这样更改代码,

class PersonConfiguration : EntityTypeConfiguration<Person>
    {
        public PersonConfiguration()
        {
            ToTable("Persons");

            this.HasMany(i => i.Seminars)
                .WithMany(c => c.Persons)
                .Map(mc =>
                {
                    mc.MapLeftKey("SemID");
                    mc.MapRightKey("RefID");
                    mc.ToTable("QualRef");
                });
        }
    }

//...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {


            modelBuilder.Configurations.Add(new PersonConfiguration());
            base.OnModelCreating(modelBuilder);
        }

Try changing the code like this,

class PersonConfiguration : EntityTypeConfiguration<Person>
    {
        public PersonConfiguration()
        {
            ToTable("Persons");

            this.HasMany(i => i.Seminars)
                .WithMany(c => c.Persons)
                .Map(mc =>
                {
                    mc.MapLeftKey("SemID");
                    mc.MapRightKey("RefID");
                    mc.ToTable("QualRef");
                });
        }
    }

//...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {


            modelBuilder.Configurations.Add(new PersonConfiguration());
            base.OnModelCreating(modelBuilder);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文