相关疑难解决方法(0)

EF codefirst:我应该初始化导航属性吗?

我曾经看过一些书(例如编程实体框架代码Julia Lerman)定义了他们的域类(POCO)而没有初始化导航属性,如:

public class User
{
    public int Id { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<Address> Address { get; set; }
    public virtual License License { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

生成POCO时,其他一些书籍或工具(例如Entity Framework Power Tools)初始化类的导航属性,如:

public class User
{
    public User()
    {
        this.Addresses = new IList<Address>();
        this.License = new License();
    }
    public int Id { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<Address> Addresses { …
Run Code Online (Sandbox Code Playgroud)

c# domain-driven-design entity-framework navigation-properties ef-code-first

59
推荐指数
2
解决办法
2万
查看次数

Code First添加到集合中?如何使用Code First和存储库?

编辑:这只发生在具有存储库的大型项目中.有没有人使用EF4和CodeFirst方法并使用存储库?请建议我.

你好.我目前正在使用EF4 CodeFirst Classes.在我的测试项目中,我有两个类,作者和书(作者得到书).我正在尝试做的是我在我的作者类中有一个AddBook,但似乎不能像我不能将它添加到集合中..这里是我的类和两个不同的例外.

 public class Book
{
    public virtual int BookId { get; set; }
    public virtual string Title { get; set; }
    public virtual Author Author { get; set; }
}

public class Author
{
    public virtual int AuthorId { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }

    public Author()
    {
        Books = new Collection<Book>();
    }

    public void AddBook(Book book)
    {
        book.Author = this;
        Books.Add(book);
    }
} …
Run Code Online (Sandbox Code Playgroud)

entity-framework code-first entity-framework-4 asp.net-mvc-2

14
推荐指数
3
解决办法
2万
查看次数

为什么EF导航属性返回null?

我有两个型号1)

public class Indicator
{
    public long ID { get; set; }
    public string Name { get; set; }
    public int MaxPoint { get; set; }
    public string Comment { get; set; }
    public DateTime DateChanged { get; set; }
    public DateTime DateCreated { get; set; }

    public virtual IList<CalculationType> CalculationTypes { get; set; }
    public virtual IList<TestEntity> TestEntitys { get; set; }
    public virtual IndicatorGroup IndicatorGroup { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

2)

public class CalculationType
{
    public long ID …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc entity-framework

10
推荐指数
3
解决办法
1万
查看次数

如何确保在访问延迟加载集合属性时加载它们的支持字段?

概括

将 EF Core 配置为延迟解析实体的导航属性时,导航属性及其可能具有的任何支持字段null直到首次访问该属性为止。这是有道理的。

但是,如果您需要在访问导航属性之前访问实体本身内的支持字段,这可能会出现问题。在这种情况下,该字段将被null抛出NullReferenceException

在某些情况下,需要使用支持字段而不是属性来执行操作。在这些情况下,我想知道是否有一种干净的方法来确保正确加载支持字段,而不必:

  1. 牺牲实体的良好封装性
  2. 在实体中放置一些代码,这些代码只是为了让它能够与底层持久性逻辑很好地配合

例子

集合导航的常见模式是拥有可变集合类型的支持字段并将其公开为只读集合。例如,考虑这样的实体:

public class ProductConsultant
{
    private readonly List<Discretionary> _discretionaries = null!;

    protected ProductConsultant()
    {
        // Required by EF
    }

    // Public constructor goes here

    public virtual IReadOnlyCollection<Discretionary> Discretionaries => _discretionaries.AsReadOnly();

    public void RemoveDiscretionaryWithId(int discretionaryId)
    {
        _discretionaries.RemoveAll(x => x.Id == discretionaryId);
    }
}
Run Code Online (Sandbox Code Playgroud)

RemoveDiscretionaryWithId如果我在第一次访问之前调用Discretionaries,代码将抛出一个NullReferenceException. 我无法直接Discretionaries在该方法中使用,因为我需要改变集合,而只读包装器阻止了这种情况。

为了解决这个问题,我显然需要做一些像这样的非常黑客的事情:

public class ProductConsultant
{
    private readonly List<Discretionary> _discretionaries = null!;

    protected ProductConsultant() …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core ef-core-3.1

5
推荐指数
0
解决办法
761
查看次数

我应该在构造函数中初始化集合

我和many-to-many之间有关系FooBar

这是一个简化的Foo

public class Foo
{
    public virtual ICollection<Bar> Bars {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

我想保存一个附带一些条形的新foo:

var foo = new Foo();
foo.Bars.Add(bar1); //I'll get an error that if Bars is not initialized
repo.Insert(foo);
repo.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

我应该在构造函数中初始化Bars,这应该怎么做?(使用EF4 CTP5 codefirst)

entity-framework-4

4
推荐指数
1
解决办法
2042
查看次数

实体框架将项目添加到ICollection错误

我有两个简单的类,User并且Task:

class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

class Task
{
    public int TaskId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedAt { get; set; }
    public virtual ICollection<User> Followers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Task类有一个属性Followers是一个ICollection<User>

这是db上下文类:

class MyContext : DbContext
    {
        public DbSet<User> Users { get; set; } …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework ef-code-first

3
推荐指数
1
解决办法
7660
查看次数