Tag: entity framework

entity framework代码优先 – 使用MySql创建数据库?

我只想尝试使用Entity Framework而不是NHibernate。 我正在使用MySql数据库。 所以我用代码第一个系统做了很少的测试。 MsSql数据库一切正常,模式是自动生成的。 但后来我用MySql尝试了它,并且模式生成不起作用。 public class Context : DbContext { public Context() { } public Context(string str) : base (str) { } public DbSet Users { get; set; } } public class User { public int Id { get; set; } public string Name { get; set; } } class Program { static […]

使用EF5类将DataGridView绑定到数据库而不将更改发送到数据库

我正在使用Entity Framework 5.0 + .NET 4.5 我使用EF Model第一种方法创建了数据库,我想使用EF类将DataGridView绑定到数据库,以便自动同步对数据库或DataGridView任何更改。 这是我的代码: //form level fields private BindingList _products; private BindingSource _productSource = new BindingSource(); … in the form load event //load the data from database using EF classes var tmp = _context.BaseCategorySet.OfType().ToList(); //converting to IBindingList _products = new BindingList(tmp); _products.AllowEdit = true; _products.AllowNew = true; _productSource.DataSource = _products; […]

entity framework – 在程序集中找不到迁移配置类型

我在C#项目中有多个dbcontexts,当我指定完整命令时,我正在尝试启用迁移,即: “Enable-Migrations -ContextTypeName Models.Account.AccountDetailDbContext” 使用配置类创建迁移文件夹 然后我收到一条消息: “检查上下文是否以现有数据库为目标……” 然后 “在程序集’Portal.WebUI’中找不到迁移配置类型’Portal.WebUI.Migrations.Configuration’。” 即使它刚刚创建了文件,也无法找到它。 有人可以帮忙吗? 我在包管理器控制台中选择了正确的项目 我已经尝试使用-verbose命令,但它没有提供其他信息 如果我将dbcontexts和类复制到一个新项目中,那么它一切正常,所以它必须是这个现有项目中的某个东西,它使迁移失败,但我不知道它是什么。 提前感谢您的任何答案

使用Entity Framework使用动态参数执行SQL Server存储过程

我们想使用EF执行存储过程。 我们的方法是动态提供输入参数,如下所示: PropertyInfo[] props = search.GetProperties(); foreach (var item in props) { object itemValue = item.GetValue(SearchCondition, null); if (itemValue == null) parameterList.Add(new SqlParameter(string.Format(“@{0}”, item.Name), DBNull.Value)); else parameterList.Add(new SqlParameter(string.Format(“@{0}”, item.Name), itemValue.ToString())); } 这是其他常见的输入: parameterList.Add(new SqlParameter(“@order”, (object)”desc”)); parameterList.Add(new SqlParameter(“@Page”, page)); parameterList.Add(new SqlParameter(“@PageSize”, rows)); 还有输出参数: parameterList.Add(new SqlParameter() { DbType = DbType.Int32, ParameterName = “@TotalCount out”, SqlValue = DBNull.Value, […]

使用entity framework添加具有新导航属性的新实体

我有一个类模型允许我将一个functionImage添加到ProductType实体。 单个ProductType类定义标识引用特定Image的HomePageImageId和HomePageImage导航属性。 我有一个Image类定义,其中包含图像的所有META信息(即宽度,高度,格式类型,名称等)。 我还有一个ImageData类定义,其中包含实际的图像数据。 这与通过上述Image类可获得的元信息具有FK关系。 所以这就是EF实体的样子。 [DataContract] [Table(“ProductTypes”)] public class ProductType : IEntity { [Key] [DataMember] [Column(“Id”)] public Int64 Id { get; set; } [DataMember] [DataType(DataType.Text)] [Column(“Name”)] public String Name { get; set; } [DataMember] [DataType(DataType.MultilineText)] [Column(“Description”)] public String Description { get; set; } [DataMember] [DataType(DataType.MultilineText)] [Column(“Excerpt”)] public String Excerpt { get; set; } [DataMember] […]

entity framework – 在部署实体对象上下文后加载引用密钥

我正在使用ASP.Net / WebForms / Entity Model / Framework 3.5 这是我的项目的简单结构Forms> BLL> DAL(使用实体模型) 这是我的DAL的片段 public class MyDAL : IDisposable { private MyEntities db; public BaseDAL() { db = new MyEntities(); } public User GetUserByID(int userId) { try { IQueryable objUser = null; objUser = from res in db.Users where res.UserId == userId select res; return objUser.FirstOrDefault(); […]

使用Entity Framework执行简单查询时出现严重的性能问题

我有一个相当通用的CRUD webapp,它根据几个数据库表的内容动态生成页面。 我正在使用Entity Framework 4.0将这些数据从数据库中提取出来,但是我遇到了严重的性能问题。 我已经设法迭代到一个足够包含的问题,我可以在下面详述。 我有一个包含页面表单列表(~200)的表。 每个表单都有一个或多个字段 (总共约4000个),每个字段可能有一些参数 (总共约16000个)。 我在下面附上了我的模型的截图: 关联的实体对象如下: public class Form { public int FormID { get; set; } public string FormName { get; set; } public IList FormFields { get; set; } } public class FormField { public int FieldID { get; set; } public string FieldName { get; set; […]

如何在没有显式引用的情况下访问打开的DbContext?

我现在一直在使用entity framework,并且我遇到了两个场景,其中两个上下文会尝试访问同一个实体等,所以我想知道是否我可能没有打开/关闭我的dbcontexts以最好的方式。 目前我基本上在每个控制器上打开一个DbContext,就像最初设置基本MVC应用程序一样,这意味着我在控制器上有一个私有dbcontext字段,并且我覆盖控制器的dispose方法来调用上下文中的dispose。 但是我有时也会在我的其他一些类中对db进行查询,这些类可以从控制器中调用,也可以打开上下文。 有没有办法在没有显式处理程序的情况下访问开放上下文? 通过十几种不同的方法传递DbContext引用对我来说真的没有意义。

如何使用lambda正确访问链接表字段?

我试图使用以下lambda代码访问链接表字段: DateTime dtStartDate = “1/1/2013”; // or some date var jobs = db.jobs.Include(d => d.docs) .Where(d => d.docs.duedate >= dtStartDate); 以下是SQL Server中的表关系键: jobs.JobID = docs.JobID // Note: Check Existing Data = No. 那么当我尝试在上面的第一行和第二行代码中执行以下操作时,上面的代码怎么会不起作用: // errors here // .duedate can’t be found through the .Include() table, docs d.docs.duedate 错误说: ‘System.Collections.Generic.ICollection’不包含’duedate’的定义,也没有扩展方法’duedate’接受类型’System.Collections.Generic.ICollection’的第一个参数(你是否缺少using指令)或汇编参考?) entity framework生成的类代码: public partial class jobs […]

使用数据MVC 3 .NET填充多选列表

我一直在谷歌和Stackoverflow上搜索关于如何使用可以选择的值填充多重选择框的解决方案。 但没有运气。 public class PriceObj { public int ID { get; set; } public decimal Price { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection PriceGroup {get; set;} } public class PriceGroupObj { public int ID { get; set; } public string Name […]