ASP.net MVC Core 2实体框架返回null [英] ASP.net MVC Core 2 Entity Framework returns null

查看:78
本文介绍了ASP.net MVC Core 2实体框架返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个ASP.net Core 2应用程序.我想在项目中使用现有的MS-SQL数据库.

I´m creating an ASP.net Core 2 Application. There is an existing MS-SQL Database which I want to use in the Project.

我使用以下命令添加了数据库:

I have added the Database with the following command:

dotnet ef dbcontext支架 服务器= XXXX;数据库= XXXX;受信任的连接=正确;集成安全性= False;用户 Id = XXXX; Password = XXXX"Microsoft.EntityFrameworkCore.SqlServer --output-dir模型/DB

dotnet ef dbcontext scaffold "Server=XXXX;Database=XXXX;Trusted_Connection=True;IntegratedSecurity=False;User Id=XXXX;Password=XXXX" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models/DB

在数据库中有以下连接:

In the Database there is the following Connection:

人员----- PersonsGroup关系-----组

Persons ----- PersonsGroupRelationships ----- Groups

在Entity Framework中,类似

In the Entity Framework something like

db.Persons.First().Groups

应该工作. (在其他MVC 5应用程序中,它可以正常工作),但是只有Persons属性可以正确加载.该连接始终包含空值.

should work. (In other MVC 5 Application it´s working perfect) But only the Persons attributes are loading correctly. The connection contains always null.

即使我在实体框架中选择的人员在代码中具有组关系",我仍会返回空值.

Even though the Person I have selected in the Entity Framework has Group Relationships in the code I get a null value returned.

您可以看到我所介绍的普通属性可以正常工作,但是数据库中的每个关系都存在问题.

As you can see the normal attributes that I have covered are working correct but there is a problem with every Relationship in the Database.

在模型中实现了连接:

Persons.cs包含:

Persons.cs contains:

public Persons()
        {
            Groups = new HashSet<Groups>();
            PersonsCompaniesRelationships = new HashSet<PersonsCompaniesRelationships>();
            PersonsGroupsRelationships = new HashSet<PersonsGroupsRelationships>();
            SrmStudenten = new HashSet<SrmStudenten>();
        }

        ...OtherStuff...

        public Groups ResponsibilityGroupNavigation { get; set; }
        public ICollection<Groups> Groups { get; set; }
        public ICollection<PersonsCompaniesRelationships> PersonsCompaniesRelationships { get; set; }
        public ICollection<PersonsGroupsRelationships> PersonsGroupsRelationships { get; set; }
        public ICollection<SrmStudenten> SrmStudenten { get; set; }

DB-Context文件包含:

The DB-Context file contains:

modelBuilder.Entity<Persons>(entity =>
            {
                ...OtherStuff...

                entity.HasOne(d => d.ResponsibilityGroupNavigation)
                    .WithMany(p => p.Persons)
                    .HasForeignKey(d => d.ResponsibilityGroup)
                    .HasConstraintName("FK_Persons_Groups");
            });

我希望你们中的任何人都可以帮助我:-)

I hope anyone of you can help me :-)

更新:

我已经更改了代码,就像Microsoft Doc告诉我进行延迟加载

I have changed the Code like the Microsoft Doc is telling me for Lazy Loading

在上下文文件中,我添加了:

In the Context File i have added:

optionsBuilder
    .UseLazyLoadingProxies()

在课堂上,我将其更改为虚拟:

In the classes i changed it to virtual:

    public virtual ICollection<Groups> Groups { get; set; }
    public virtual ICollection<PersonsCompaniesRelationships> PersonsCompaniesRelationships { get; set; }
    public virtual ICollection<PersonsGroupsRelationships> PersonsGroupsRelationships { get; set; }
    public virtual ICollection<SrmStudenten> SrmStudenten { get; set; }

不幸的是,结果仍然是这样的:

Unfortunately the result still looks like this:

推荐答案

我遇到了此类问题,并且找到了解决方法:

I faced this kind of issue and i got a way steps:

第01步:为

 <ItemGroup>
    <PackageReference Include="EntityFramework" Version="6.2.0" />

     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0">
     <PrivateAssets>all</PrivateAssets>
     <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
     </PackageReference>
  </ItemGroup>

使用兼容的这些参考来调整支持的.net版本. 或者,您可以从nuget中找到所需的引用

Adjust supported .net version with compatible these referece. Or you can find your desired references from nuget

使用Microsoft.EntityFrameworkCore添加引用;不使用system.data.entity; 使用system.data.entity删除; .include

add referece using Microsoft.EntityFrameworkCore; do not using system.data.entity; remove using system.data.entity; reference for .Include

尝试一下.节省了我的时间. 谢谢

Let's try. It's saved my time. Thanks

这篇关于ASP.net MVC Core 2实体框架返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆