首页 新闻 搜索 专区 学院

EF6 一对一映射

0
悬赏园豆:200 [已解决问题] 解决于 2021-01-10 12:12



主要解决黑窗口的报错

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

万分感谢的主页 万分感谢 | 初学一级 | 园豆:4
提问于:2021-01-09 13:24
< >
分享
最佳答案
0

最好是使用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"
  }
}
收获园豆:200
Jonny-Xhl | 小虾三级 |园豆:662 | 2021-01-09 22:12




我已经解决了,还是谢谢你,我的结构,dbset用的是泛型。

万分感谢 | 园豆:4 (初学一级) | 2021-01-10 12:10

@万分感谢: Context不添加DbSet<TEntity>能行吗? 你后面这个只是一个仓储操作,与Migration无关。

Jonny-Xhl | 园豆:662 (小虾三级) | 2021-01-10 21:30

@Jonny-Xhl: 我没有用Migration,我是用的DbFactory

万分感谢 | 园豆:4 (初学一级) | 2021-01-23 10:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册