使用Entity Framework 5,Visual Studio 2010和Entity Framework Power Tools(Beta 2)扩展.
这是我的数据库表结构:

我使用了上述扩展的逆向工程师代码优先功能,它生成了POCO类和一些"映射"文件(不确定这是否是正式用法)和单个DbContext派生类.除了我接下来描述的更改之外,所有这些生成的类都是由电动工具生成的.
在Category.cs文件中,我添加了以下代码以帮助展平对象图:
private ICollection<Product> m_Products = null;
public ICollection<Product> Products
{
get
{
if (m_Products == null)
{
m_Products = new List<Product>();
foreach (var categoryProduct in CategoryProducts)
{
m_Products.Add(categoryProduct.Product);
}
}
return m_Products;
}
set { m_Products = value; }
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常,我知道必须与映射有关,但我不能完全弄清楚这一点.
Unhandled Exception: System.Data.EntityCommandExecutionException: An error occurred while
executing the command definition. See the inner exception for details.
---> System.Data.SqlClient.SqlException:
Invalid …Run Code Online (Sandbox Code Playgroud) entity-framework visual-studio-2010 ef-code-first entity-framework-5
我被困在我的项目中,非常感谢您的帮助。我正在使用EF 5 Code-First方法。
这是我的基本实体:
...
public virtual Guid Id
{
get
{
return _id;
}
protected set { }
}
public virtual bool IsEnabled { get; set; }
...
Run Code Online (Sandbox Code Playgroud)
而我的实体:
...
public string CountryName { get; set; }
public string CountryCode { get; set; }
public Coordinate CountryCoordinate { get; set; }
public virtual ICollection<City> Cities
{
get
{
if (_cities == null)
{
_cities = new HashSet<City>();
}
return _cities;
}
private set
{
_cities = new …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用EF5(代码优先).我有一个包含一些延迟加载字段的表.
public class TestEntity
{
public int Id { get; set; }
public virtual TestEntity2 SubEntity2 { get; set; }
public virtual TestEntity3 SubEntity3 { get; set; }
private ICollection<SubEntity4> _subEntities;
public ICollection<SubEntity4> SubEntities
{
get { return _subEntities ?? (_subEntities = new Collection<SubEntity4>()); }
protected set { _subEntities = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
当我从数据库SubEntity2和SubEntity3读取此内容时正好加载,但SubEntities集合不会加载,它始终保持Count = 0.所以我强迫这样加载:
db.Entry(queryResult).Collection(rr => rr.SubEntities).Load();
Run Code Online (Sandbox Code Playgroud)
但据我所知,这个集合应该在第一次调用时由EF自动加载,就像SubEntity2和SubEntity3一样.为什么不与收藏合作?
我用来读取数据库的代码示例:
using (var db = new TestContext(_connection, false))
{
var query = from r in db.SubEntities
where r.Id == …Run Code Online (Sandbox Code Playgroud) 我在我的Entity Framework 5模型中定义了一个枚举,我用它来定义表格中字段的类型,例如
public enum PrivacyLevel : byte {
Public = 1,
FriendsOnly = 2,
Private = 3,
}
Run Code Online (Sandbox Code Playgroud)
我有一个表Publication有一个tinyint字段PrivacyLevel,我已经在EF模型中映射使用PrivacyLevel上面定义的类型,使用此处描述的方法.
但我也希望能够为枚举的每个值显示字符串描述.这是我过去为枚举做的,通过使用Description属性进行装饰,例如
public enum PrivacyLevel : byte {
[Description("Visible to everyone")]
Public = 1,
[Description("Only friends can view")]
FriendsOnly = 2,
[Description("Only I can view")]
Private = 3,
}
Run Code Online (Sandbox Code Playgroud)
我有一些代码通过检查它们是否具有Description属性将枚举转换为字符串,并且运行良好.但是在这里,因为我必须在我的模型中定义枚举,底层代码是自动生成的,并且我没有任何稳定来装饰它们.
任何解决方法的想法?
我在使用实体框架尝试使用重复参数执行SQL查询时遇到了麻烦.
查询是关键字搜索,它在不同的表中查找,因此多次使用相同的参数.我正在使用LIKE语句(是的,我知道我应该使用FULLTEXTSEARCH,但我现在没有时间).
我已经尝试了这里解释的所有语法:如何在存储过程中使用DbContext.Database.SqlQuery <TElement>(sql,params)?EF Code First CTP5 并且它们都不会使查询工作(我得到零返回行).
我甚至尝试在运行时构建一个字符串数组,其长度等于参数在查询中重复的次数,然后使用关键字搜索项填充数组的所有元素.然后我将其作为对象[]参数传递给我.也没用.
唯一有效的方法是进行搜索和替换,这显然是一个坏主意,因为参数来自文本输入,我将容易受到SQL注入攻击.
工作代码(易受SQL注入攻击,但查询返回行):
//not the real query, but just for you to have an idea
string query =
"SELECT Field1, " +
" Field2 " +
"FROM Table1 " +
"WHERE UPPER(Field1) LIKE '%{0}%' " +
"OR UPPER(Field2) LIKE '%{0}%'";
//keywordSearchTerms is NOT sanitized
query = query.Replace("{0}", keywordSearchTerms.ToUpper());
List<ProjectViewModel> list = null;
using (var context = new MyContext())
{
list = context.Database.SqlQuery<ProjectViewModel>(query, new object[] { }).ToList();
}
return …Run Code Online (Sandbox Code Playgroud) 我有以下通用代码来更新断开连接的实体:
public T UpdateItem(T entity)
{
this._dbSet.Attach(entity);
this._dbContext.Entry(entity).State = System.Data.EntityState.Modified;
this._dbContext.SaveChanges();
return entity;
}
Run Code Online (Sandbox Code Playgroud)
如果我的实体包含导航属性,那么这些属性不会附加并设置为已修改.有没有办法可以更改此通用方法以附加和设置为修改,所有导航属性?
我们将使用MVC 4和EF 5为私营公司开发一个中大型定制Web应用程序.
早期分析和以前在该业务领域的经验表明,它将拥有超过150个表/实体,因为我们的客户不是软件工程师,我们知道我们的数据模型将在项目进展中多次改变.
现在,根据以下内容,我的Q是哪种方法对我们更好:
1)由于会发生许多变化,因此更新数据模型和数据存储的工作量减少.
2)创建数据存储所需的时间更少,并有助于更快地推进项目.
注意:此应用程序将使用大量数据(对于某些实体,10k,100k对象).但是,它将很少(有时可能没有)并发请求和在线用户.
提前致谢
database-design ef-code-first ef-database-first asp.net-mvc-4 entity-framework-5
我有一个JSON字符串,我将其保存为EF权利.
Report result = js.Deserialize<Report>(json);
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用相同的ID更新我的上下文中的实体,以获得我的deserlized值.
var reportToUpdate _entities.Reports.Single(x => x.Id == result.Id)
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情
reportToUpdate = set all values to the values from result
context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我想避免做这样的事情:
report.param1 = result.param1
report.param3 = result.param3
report.param3 = result.param3
Run Code Online (Sandbox Code Playgroud)
因为这个实体有大约50个属性.
我正在制作一个简单的插入,更新,删除,选择数据的应用程序,Entity Framework
我已经插入,删除和选择所有数据.
现在我想选择具有两个字段的过滤器的条件
.例如:我有表格
userid
username
password
email
Run Code Online (Sandbox Code Playgroud)
现在需要选择 where email = "" and password = ""
我知道如何在SQL中编写查询但没有线索entity framework.
还需要将此结果存储在数据表和循环解决方案中,以用于学习目的.
这可以帮助许多初学者
使用Identity 2.0注册用户后,在数据库中创建用户,Account控制器中有代码创建身份并在用户签名.这是我收到错误的时候.以下是产生错误的代码:
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)
这是错误:
实体类型IdentityRole不是当前上下文的模型的一部分.
我的模型看起来像这样:
namespace MyApp.Models
{
public class ApplicationUser : IdentityUser
{
[Required]
[StringLength(50)]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
public string LastName { get; set; }
}
public class ApplicationRole : IdentityRole
{
[Required]
[StringLength(50)]
public string ProperName { get; set; }
[Required]
public string Description { get; set; }
}
public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public MyAppDb()
: base("MyAppDb")
{
} …Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework entity-framework-5 asp.net-identity asp.net-mvc-5.1