标签: navigation-properties

实体框架 - 手动添加导航属性

我从我的数据库生成了一个实体框架模型(4.0).我没有设计数据库,也没有对模式的任何控制,但是有一些表没有定义外键约束,但是定义了隐式关系.

例如:

我有一个名为People的表,其中包含以下列:GenderID RaceID

有Gender和Race的表,但People表中没有外键.

当我导入模型时,它没有为这些关系添加导航属性.我尝试手动添加它,但禁用了"从角色到角色".我不确定如何自己添加关系.我该怎么做呢?

c# entity-framework navigation-properties entity-framework-4

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

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万
查看次数

导航属性应该是虚拟的 - 在ef核心中不需要?

我记得在EF 导航属性应该是虚拟的:

public class Blog 
{  
    public int BlogId { get; set; }  
    public string Name { get; set; }  
    public string Url { get; set; }  
    public string Tags { get; set; }  

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

但我看看EF Core并不认为它是虚拟的:

public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# virtual entity-framework navigation-properties entity-framework-core

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

为什么导航属性在EF中默认为虚拟

我在EF 6.x中使用了以下POCO类.

我的问题:为什么"博客"实体下的"帖子"的导航属性被声明为虚拟?

public class Blog 
{  
    public int BlogId { get; set; }  
    public string Name { get; set; }  
    public string Url { get; set; }  
    public string Tags { get; set; }  

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

entity-framework navigation-properties

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

实体框架(Database-First)与同一个表命名约定控件的多个关系

我们假设我们遇到这种情况:

数据库中的表:

Country (id, country_name), Person (id, login), CountryManager (id_country, id_person), CountryStakeholder (id_country, id_person)

如果我们必须从数据库创建模型,使用Entity Framework Database-First,在VS中我们有一个这样的类:

class Country {

int id;
string country_name;

virtual ICollection<Person> Person1; // Navigation Properties
virtual ICollection<Person> Person2; // ---------||----------

}
Run Code Online (Sandbox Code Playgroud)

我已经简化了很多代码,但希望你明白了.

似乎当Entity Framework处理外键时,它会创建通用导航属性.是否有可能控制按名称创建导航属性的方式?不幸的是,Person1,Person2不是很解释.

c# entity-framework naming-conventions foreign-key-relationship navigation-properties

20
推荐指数
1
解决办法
5318
查看次数

OData和WebAPI:模型中不存在导航属性

我正在尝试使用Entity Framework,WebAPI,OData和Angular客户端组合一个简单的玩具项目.一切都运行正常,除了我放在我的一个模型上的导航属性似乎没有工作.当我使用$ expand调用我的API时,返回的实体没有它们的导航属性.

我的课程是狗和老板,看起来像这样:

    public class Dog
{
    // Properties
    [Key]
    public Guid Id { get; set; }
    public String Name { get; set; }
    [Required]
    public DogBreed Breed { get; set; }
    public int Age { get; set; }
    public int Weight { get; set; }


    // Foreign Keys
    [ForeignKey("Owner")]
    public Guid OwnerId { get; set; }

    // Navigation
    public virtual Owner Owner { get; set; }
}

    public class Owner
{
    // Properties
    public Guid Id { get; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework navigation-properties odata asp.net-web-api

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

如何使用单向导航建模实体框架实体/映射

使用EF 5,Code First.

我想对我的实体进行建模,使得导航属性仅存在于关系的一侧.

所以如果我有一个表格Widget和一个表格WidgetType:

public class Widget
{
    public int Id { get; set; }
    public int WidgetTypeId { get; set; }
    public WidgetType WidgetType { get; set; }
}

public class WidgetType
{
    public int Id { get; set; }
    //note there is no collection of Widgets here
}

public class WidgetMap : EntityTypeConfiguration<Widget>
{
    public WidgetMap()
    {
        HasKey(t => t.Id);
        //totable, etc.

        HasRequired(t => t.WidgetType); //what else is needed?
    }
}
Run Code Online (Sandbox Code Playgroud)

我永远不会想要从widgetType的角度来获取小部件,所以对我来说无论如何都没有WidgetType实体上的导航属性.

如何在不必向WidgetType添加属性的情况下完成代码示例中提到的映射代码?这可能吗?

c# entity-framework navigation-properties ef-code-first entity-framework-5

12
推荐指数
2
解决办法
4850
查看次数

使用Automapper,将DTO映射回实体框架,包括引用的实体

我有使用Entity Framework 5持久化的POCO域实体.它们是使用存储库模式从DbContext获得的,并通过UoW模式暴露给RESTful MVC WebApi应用程序.POCO实体是代理并且是延迟加载的.

我将我的实体转换为DTO,然后再将它们发送给客户端.我正在使用Automapper执行此操作,它似乎可以正常工作,Automapper将代理POCO映射到DTO,保持导航属性不变.我正在使用以下映射:

    Mapper.CreateMap<Client, ClientDto>();
Run Code Online (Sandbox Code Playgroud)

域/ DTO对象的示例:

[Serializable]
public class Client : IEntity
{
    public int Id { get; set; }

    [Required, MaxLength(100)]
    public virtual string Name { get; set; }

    public virtual ICollection<ClientLocation> ClientLocations { get; set; }

    public virtual ICollection<ComplianceRequirement> DefaultComplianceRequirements { get; set; }

    public virtual ICollection<Note> Notes { get; set; }
}

public class ClientDto : DtoBase
{
    public int Id { get; set; }

    [Required, MaxLength(100)]
    public string Name { get; set; …
Run Code Online (Sandbox Code Playgroud)

entity-framework dto automapper navigation-properties asp.net-web-api

11
推荐指数
1
解决办法
6186
查看次数

我可以通过委托给EF中的存储过程来延迟加载导航属性吗?

我有以下客户类:

public class Customer 
{
    public long Id { get; set; }

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

我的数据库有Customers和Orders表,但没有外键关系.使用存储过程获取客户的订单,该存储过程获取客户ID并返回订单行.我无法修改数据库.

我知道如何从Entity Framework调用存储过程,但是,是否可以使用流畅的API配置DbContext,以便访问客户对象的Orders集合会通过调用存储过程来延迟加载实体?

我正在使用最新版本的EF.

stored-procedures entity-framework navigation-properties

9
推荐指数
1
解决办法
553
查看次数

实体框架核心:私有或受保护的导航属性

是否有可能在EFCore中使用私有或受保护的访问级别定义导航属性,以使这种代码工作:

class Model {
   public int Id { get; set; }
   virtual protected ICollection<ChildModel> childs { get; set; }  
}
Run Code Online (Sandbox Code Playgroud)

c# navigation-properties entity-framework-core .net-core asp.net-core

9
推荐指数
1
解决办法
4334
查看次数