ASP.net MVC Core 2 Entity Framework returns null

柔情痞子 提交于 2019-12-14 03:29:18

问题


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 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:

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

In the Entity Framework something like

db.Persons.First().Groups

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.

In the Model the Connection is implemented:

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; }

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 :-)


Update:

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:


回答1:


Do the following

IQueryable<Person> query = dbContext.Set<Person>().Include(x=> x.Groups);
var firstItem = query.FirstOrDefault().Groups;

You can alternatively do:

Person query = dbContext.Set<Person>().FirstOrDefault();
DbContext.Entry<Person>(query).Collection(x=> x.Groups).Load();



回答2:


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

Step 01: add packages for

 <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>

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

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



来源:https://stackoverflow.com/questions/49320736/asp-net-mvc-core-2-entity-framework-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!