EF6 一对一映射

2021-01-10 11:27发布



主要解决黑窗口的报错

users这个类有一个邀请人属性,为什么这个能直接映射到数据库,而article不行,
试过在next属性上加ForeignKey特性也不行

标签:
1条回答
老娘就宠你
2楼-- · 2021-01-10 12:05

最好是使用Flunt Api指定一下。

我测试的效果:

  • DbContextFactory
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace csblog_133043
{
    public class CnBlog133043MigrationsContextFactory : IDesignTimeDbContextFactory<CnBlog133043Context>
    {
        public CnBlog133043Context CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<CnBlog133043Context>()
                .UseSqlServer(configuration.GetConnectionString("Default"));

            return new CnBlog133043Context(builder.Options);
        }
        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
  • DbContext
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace csblog_133043
{
    [ConnectionStringName("Default")]
    public class CnBlog133043Context : AbpDbContext<CnBlog133043Context>
    {
        public CnBlog133043Context(DbContextOptions<CnBlog133043Context> options)
            : base(options)
        {
        }

        public DbSet<Article> Articles { get; set; }

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

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Users>(u=>
            {
                u.ToTable(nameof(Users));

                u.ConfigureByConvention();

                u.HasOne(n => n.Referrer).WithOne();
            });

            modelBuilder.Entity<Article>(a=>
            {
                a.ToTable(nameof(Article));

                a.ConfigureByConvention();

                a.HasOne(n => n.Next).WithOne();
                a.HasOne(n => n.Previous).WithOne();
            });
        }
  • Users Article
using System;
using System.Collections.Generic;
using System.Text;

namespace csblog_133043
{
    public class Users
    {
        public int Id { get; set; }

        public Users Referrer { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Text;

namespace csblog_133043
{
    public class Article
    {
        public int Id { get; set; }
    
        public Article Next { get; set; }

        public Article Previous { get; set; }
    }
}

  • 配置文件
{
  "ConnectionStrings": {
    "Default": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=cnblog;Integrated Security=True"
  }
}
查看更多
登录 后发表回答