我有一个使用Entity Framework 5.0 Code First的MVC4 Web应用程序.
在Global.asax.cs中,我有一个初始化Entity.Database的引导程序,强制初始化数据库并初始化成员资格数据库.代码是这样的:
System.Data.Entity.Database.SetInitializer(new DatabaseContextInitializer());
Database.Initialize(true);
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
Run Code Online (Sandbox Code Playgroud)
DatabaseContextInitializer目前非常简单:
public class DatabaseContextInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
{
protected override void Seed(DatabaseContext dbContext)
{
base.Seed(dbContext);
db.Set<Workout>().Add(new Workout {Id = 1, Name = "My First workout user1"})
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法通过以下方式为会员创建用户:
WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, "UserProfile", "UserId", "UserName", autoCreateTables: true);
Run Code Online (Sandbox Code Playgroud)
因为我有一个问题,即没有创建数据库.如何使用Entity Framework 5.0和Asp.Net MVC 4初始化数据库的某些默认用户?
asp.net-mvc entity-framework asp.net-membership asp.net-mvc-4 entity-framework-5
是否可以全局设置Entity Framework DbContext以datetime2用于System.DateTime使用Code-First模型时的所有属性?
我可以使用HasColumnType()方法为每个列执行此操作,但对于现有的代码库,我想要一个全局解决方案.
如果我使用Entity Framework(v5.0)数据库第一种方法,那么使用数据注释进行验证的最佳方法是什么?
这是我的Entity Framework创建的部分类:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.ComponentModel.DataAnnotations;
namespace ACore
{
using System;
using System.Collections.Generic;
public partial class PayrollMarkup_State
{
[UIHint("StatesEditor")] // <-- I added this line but it will be overwritten
public string State { get; set; }
public …Run Code Online (Sandbox Code Playgroud) 我是entities这样的数据网格视图的绑定列表:
var orders = context.Order.ToList();
BindingList<Order> orderList = new BindingList<Order>(orders);
dataGridView1.DataSource = orderList;
Run Code Online (Sandbox Code Playgroud)
用户可以直接在datagridview上编辑或添加新内容.当用户单击Save按钮时,为了优化性能,我想检索已更改/新的实体列表以执行插入/更新.我怎样才能做到这一点?
编辑定义向gridview添加新行:
BindinList<Order> orders = (BindingList<Order>)dataGridView1.Datasource;
order.Add(new Order());
Run Code Online (Sandbox Code Playgroud)
编辑2解决:
BindinList<Order> orders = (BindingList<Order>)dataGridView1.Datasource;
Order order = new Order();
context.Order.Add(order);
order.Add(order);
Run Code Online (Sandbox Code Playgroud) 有谁知道如何使用EF 5迁移为特定更新播种?我有现有的数据库,alredy已经填充了查找,并且正在开发一些审计功能.我创建了一个AuditType实体和Audit.当我调用update-database时,我不希望在我第一次创建数据库时重新添加所有种子数据.我是否只需要手动删除现有的种子数据,或者我可以使用类似于调用add-migration时创建的日期时间来命名Configuration.cs吗?
谢谢
为什么Entity Framework会生成嵌套的SQL查询?
我有这个代码
var db = new Context();
var result = db.Network.Where(x => x.ServerID == serverId)
.OrderBy(x=> x.StartTime)
.Take(limit);
Run Code Online (Sandbox Code Playgroud)
哪个产生了这个!(注意双选语句)
SELECT
`Project1`.`Id`,
`Project1`.`ServerID`,
`Project1`.`EventId`,
`Project1`.`StartTime`
FROM (SELECT
`Extent1`.`Id`,
`Extent1`.`ServerID`,
`Extent1`.`EventId`,
`Extent1`.`StartTime`
FROM `Networkes` AS `Extent1`
WHERE `Extent1`.`ServerID` = @p__linq__0) AS `Project1`
ORDER BY
`Project1`.`StartTime` DESC LIMIT 5
Run Code Online (Sandbox Code Playgroud)
我应该更改什么才能产生一个选择语句?我正在使用MySQL和Entity Framework与Code First.
无论传递给OrderBy()方法的参数类型如何,我都有相同的结果.
Total Time (hh:mm:ss.ms) 05:34:13.000
Average Time (hh:mm:ss.ms) 25:42.000
Max Time (hh:mm:ss.ms) 51:54.000
Count 13
First Seen Nov 6, 12 19:48:19
Last Seen Nov 6, 12 …Run Code Online (Sandbox Code Playgroud) 鉴于原始值,age我知道如何创建这样的表达式:
//assuming: age is an int or some other primitive type
employee => employee.Age == age
Run Code Online (Sandbox Code Playgroud)
通过做这个:
var entityType = typeof(Employee);
var propertyName = "Age";
int age = 30;
var parameter = Expression.Parameter(entityType, "entity");
var lambda = Expression.Lambda(
Expression.Equal(
Expression.Property(parameter, propertyName),
Expression.Constant(age)
)
, parameter);
Run Code Online (Sandbox Code Playgroud)
除了在有问题的属性和常量不是原始类型的情况下,这种方法很好.
如果比较是在对象之间,我将如何构造一个类似的表达式?
使用EF我可以写:
Location location = GetCurrentLocation();
employees = DataContext.Employees.Where(e => e.Location == location);
Run Code Online (Sandbox Code Playgroud)
这也有效,但如果我尝试创建相同的表达式:
var entityType = typeof(Employee);
var propertyName = "Location";
var location = GetCurrentLocation();
var parameter = Expression.Parameter(entityType, "entity"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Entity Framework的代码优先概念创建一个新数据库.但是,在运行代码时,不会创建数据库(使用DropCreateDatabaseIfModelChanges设置),尽管代码运行正常.当我尝试从数据库中获取某些内容时,我看到以下异常.

我的项目使用DataAccess具有通用服务和存储库构造的单独层进行设置.因此,我的所有实体,存储库和数据库上下文都在解决方案中的单独项目中.
我的global.asax文件包含以下代码.
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
Run Code Online (Sandbox Code Playgroud)
这应该初始化一个新的数据库,如果它不存在,对吧?
我的数据库上下文类看起来像这样;
namespace Website.DAL.Model
{
public class MyContext : DbContext
{
public IDbSet<Project> Projects { get; set; }
public IDbSet<Portfolio> Portfolios { get; set; }
/// <summary>
/// The constructor, we provide the connectionstring to be used to it's base class.
/// </summary>
public MyContext()
: base("MyConnectionString")
{
}
static MyContext()
{
try
{
Database.SetInitializer<MyContext>(new DropCreateDatabaseIfModelChanges<MyContext>());
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// This method …Run Code Online (Sandbox Code Playgroud) c# entity-framework ef-code-first asp.net-mvc-4 entity-framework-5
如果我首先使用EF 5和数据库生成数据库的.edmx模型,如何获取实体列的列表?
using (var db = new ProjectNameContext())
{
// string[] colNames = db.Users.
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是colNames [0] =="Id",colNames [1] =="FirstName"等.
我有一个包含大约16,500个城市的表的数据库,以及该数据库的EF数据模型(数据库优先).我用代码将它们预加载到内存中:
Db.Cities.Load()
Run Code Online (Sandbox Code Playgroud)
...然后在使用它们的时候,我尝试了以下每个查询:
Dim cities() As String = Db.Cities.Select(Function(c) c.CityName).ToArray
Dim cities() As String = Db.Cities.Local.Select(Function(c) c.CityName).ToArray
Run Code Online (Sandbox Code Playgroud)
第一个查询是快速的(约10毫秒),但是第二个查询第一次运行大约需要2.3秒(尽管它比第一个查询在此之后调用时更快).
这没有意义,因为SQL Server Profiler验证第一个查询是在另一台机器上命中数据库,但第二个查询不是!
我试过关掉Db.Configuration.AutoDetectChangesEnabled,我试过预先生成视图.
我能做些什么来.Local加快速度?(并非所有运行此应用程序的客户端都将位于快速LAN上.)
.net vb.net entity-framework entity-framework-5 entity-framework-4.3.1