我在尝试连接查询中的多个表时遇到错误:
指定的LINQ表达式包含对与不同上下文关联的查询的引用
它令人困惑,因为它使我看起来在查询中使用不同的上下文但我不是:
public static IQueryable<Company> GetAll(bool supportsMMAT)
{
return from c in Context.Companies
join v in Context.Vehicles on c.CompanyIdNumber equals v.CompanyIdNumber
join mt in Context.ModemTypes on v.ModemTypeId equals mt.Id
where !c.CompanyShutOff
&& (!supportsMMAT || mt.Model == "MMAT")
select c;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我正在使用EF4 CTP5代码第一种方法,如果这有任何区别......
使用EF代码首先,如何中断保存字段值以便我可以对其进行哈希处理?一个简单的例子是密码字段:
public class Account
{
private string _password;
public string Password
{
get
{
return _password;
}
set
{
_password = MyHashMethod(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这在将值保存到数据库时似乎有效,但在检索值时不起作用.
编辑:将_password = MyHashMethod(_password)更改为上面的MyHashMethod(值).需要在下面的答案中进行相同的修正.
我有像这样的课程,学生和老师
public class Course
{
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public Guid CourseId { set; get; }
public ICollection<Student> Students { set; get; }
public Teacher Teacher { get; set; }
}
public class Student
{
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public Guid StudentId { set; get; }
public ICollection<Course> Courses { set; get; }
}
public class Teacher
{
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public Guid TeacherId { get; set; }
public ICollection<Course> Courses { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图按主键获取课程如下
Course c = _unitOfWork.DbContext.Set<Course>().Find(keyValue);
Run Code Online (Sandbox Code Playgroud)
我从数据库中获取课程对象,但课程的学生和教师属性为空
我错过了什么?谢谢
此摘录代码成功地与显式Junction表创建了许多关系,其中包含其他数据.
问题:我希望能够从学生访问课程,反之亦然
(因此注释虚拟财产.但如果我取消注释,它会导致错误(见下文))
如果我没有显式创建联结表(没有其他数据),则虚拟关键字可以正常工作,因为EF会按惯例创建联结表.
题:
(EF和C#的初学者)
public class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
//public virtual Course Courses { get; set; }
}
public class Course
{
[Key]
public int CourseId { get; set; }
public string CourseName { get; set; }
//public virtual Student Students { get; set; }
}
public class Enrollment
{
[Key]
public …Run Code Online (Sandbox Code Playgroud) c# database entity-framework code-first entity-framework-4.3
我需要映射一些自动生成的类,我不想改变它们的显示(它是自动生成的!).
那么是否有可能使用实体框架的流畅API定义主键(在我的情况下为4.3)?由于NULL值,将属性处理为ComplexType会导致DbUpdateExcetion.向生成的类添加主键可能是一种解决方案,但不适合我,POCO必须保持不变.请帮助!谢谢.
目前我忽略了属性,因为......
Run Code Online (Sandbox Code Playgroud)ModelValidationException EntityType 'Attribute' has no key defined. Define the key for this EntityType. EntitySet 'Attributes' is based on type 'Attribute' that has no keys defined.
这是一个简短的示例代码:
public class Item
{
public ID {set;get;}
public Attribute[] Attributes {set;get;}
}
public class Attribute
{
public SomeComplexType misc1 {set; get;}
public SomeComplexType misc2 {set; get;}
}
public class ItemDb : DbContext
{
public ItemDb(string connection) : base(connection) {}
public DbSet<Item> Items { get; set; }
protected override void OnModelCreating(DbModelBuilder builder) …Run Code Online (Sandbox Code Playgroud) 在我的单元测试中,我想强制验证代码首先在其上有DataAnnotations的POCO.
MVC框架必须在幕后进行,基本上我想知道如何,所以我希望能够使用它.
我有一个带有我域的类的程序集-“ Domains.dll”。我将动态添加到程序集的DbContext Dbset加载类中。
public class MyContext : DbContext
{
public MyContext() : base("DBConnection"){}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Assembly assembly = Assembly.LoadFrom("Domains.dll");
var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
var list = assembly.GetTypes().OrderBy(i => i.GetType().Name);
foreach (Type item in list)
{
entityMethod.MakeGenericMethod(item)
.Invoke(modelBuilder, new object[] { });
}
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我创建数据库
context.Database.Create();
Run Code Online (Sandbox Code Playgroud)
这可行,但是我的域存在问题。我有一个上级实体类
public abstract class Entity
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
public class Person : Entity
{
public string FirstName {get ;set;}
public string LastName …Run Code Online (Sandbox Code Playgroud) 我有以下情况:
颜色等级
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
public string Hex
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)小部件类
public int ID
{
get;
set;
}
public int HeaderBackgroundColorID
{
get;
set;
}
public Color HeaderBackgroundColor
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)使用Code-First,我尝试使用HeaderBackgroundColor / HeaderBackgroundColorID字段在Widget到Color类之间创建单向关系。
通常我会在config类中这样做:
this.HasOptional(r => r.HeaderBackgroundColor )
.WithMany(m => m.Widgets)
.HasForeignKey(fk => fk.HeaderBackgroundColorID);
Run Code Online (Sandbox Code Playgroud)
但是我不喜欢将Widgets集合添加到Color类。尝试了这个:
this.HasOptional(r => r.HeaderBackgroundColor )
.WithMany()
.HasForeignKey(fk => fk.HeaderBackgroundColorID);
Run Code Online (Sandbox Code Playgroud)
但这会引发验证错误。
正确的方法是什么?
entity-framework code-first ef-code-first entity-framework-6
我在论坛上搜索了很多,但是没有找到关于这个问题的任何内容.
我在EntityFramework Code First中有2个属性:
[Column(TypeName = "Money")]
public decimal? Debit { get; set; }
[Column(TypeName = "Money")]
public decimal? Credit { get; set; }
Run Code Online (Sandbox Code Playgroud)
其中一个不应为null,但另一个应为null示例:
Debit=null;
Credit=34;
Debit=45;
Credit=null;
Run Code Online (Sandbox Code Playgroud)
另一方面,不应该将它们都设置为空或者不设置它们.是否可以使用数据注释处理此问题,还是应该通过解决方法解决它?
最好的祝福!
null entity-framework properties code-first data-annotations
我使用Code First方法开发了Entity Framework.现在我正在学习Node.js我想知道是否有办法使用Node.js和一些图书馆制作相同的代码第一种方法?我正在考虑使用MySql作为数据库.
code-first ×10
c# ×3
database ×3
.net ×1
asp.net-mvc ×1
dynamic ×1
model ×1
node.js ×1
null ×1
properties ×1
unit-testing ×1