我有两个对象类
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
// Navigation
public ICollection<Product> Products { get; set; }
}
public class Product
{
public Guid Id { get; set; }
// Navigation
public User User { get; set; }
public Guid User_Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用dataContext加载用户时,我得到的产品列表为null(这没关系).
如果我在产品列表中添加"虚拟"关键字,
public virtual ICollection<Product> Products { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我加载用户时,我也获得了产品列表.
为什么会这样?我认为"虚拟"关键字用于不加载实体,除非你明确这个(使用"包含"语句)
我想我错了
c# entity-framework lazy-loading entity-framework-4 ef-code-first
我有父/子关系中的两个实体.另外,parent包含对"main"子项的引用,因此简化模型如下所示:
class Parent
{
int ParentId;
int? MainChildId;
}
class Child
{
int ChildId;
int ParentId;
}
Run Code Online (Sandbox Code Playgroud)
我现在遇到的问题是EF似乎无法在单个操作中处理父和子的创建.我收到错误"System.Data.UpdateException:无法确定依赖操作的有效排序.由于外键约束,模型要求或存储生成的值,可能存在依赖关系."
MainChildId是可空的,因此应该可以生成父项,子项,然后使用新生成的ChildId更新父项.这是EF不支持的东西吗?
在我的数据库中,我有一个表类别,列ID,CategoryName,ParentCategoryId,其中ParentCategoryId对Category.Id有约束.
我首先使用实体框架代码,实体看起来像:
public class Category
{
public long Id { get; private set; }
public string CategoryName { get; private set; }
public long? ParentCategoryId { get; private set; }
public Category ParentCategory { get; private set; }
public virtual ICollection<Category> SubCategories { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试对此运行查询,我会得到异常:
The relationship 'ComplaintModel.FK_Complaint_Category' was not loaded because the type 'ComplaintModel.Category' is not available.\r\nThe following information may be useful in resolving the previous error:\r\nThe required property 'Category1' does not exist on the …Run Code Online (Sandbox Code Playgroud) c# mapping entity-framework relational-database ef-code-first
我试图在具有Attributes和DataAnnotation的类中声明一个复合键.
[Key]
[Column(Order=1)]
public Guid Id { get; set; }
[Key]
[Column(Order=2)]
public int Nr { get; set; }
Run Code Online (Sandbox Code Playgroud)
似乎没有这样做.我到目前为止所能找到的只是一些流畅的选择,但我只想用注释来做这件事.
为了澄清:我正在寻找一种DataAnnotation方法来创建一个主键由2个字段Id和Nr组成的表...
谢谢你的帮助安德烈亚斯