ef-code-first

Why does EF5 code first use datetime2 when inserting a nullable datetime into the database?

余生颓废 提交于 2019-12-17 13:00:02
问题 I am saving a Cart object to the database that has a nullable datetime. This is the error I get: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. There are quite a few stackoverflow posts documenting fixes to this problem. However, when code first is creating the database it will create the field as a DateTime (allow nulls). But for some reason, code first tries to insert using a DateTime2 field. I am wondering why EF creates the field one way

How do I precompile an Entity Framework Code-First Query?

非 Y 不嫁゛ 提交于 2019-12-17 12:49:39
问题 I am encountering some performance problems with my Entity Framework Code-First queries and I believe that precompilation may be the answer. If I were using "normal" Entity Framework, I would simply use the CompiledQuery.Compile method to precomiple my queries. But since I have a DbContext and not an ObjectContext, I can't get this to work. I do realize that DbContext is an IObjectContextAdapter, which gives me access to the ObjectContext, but I cannot find the method that lets me get an

LINQ To Entities + Include + Anonymous type issue

99封情书 提交于 2019-12-17 12:46:14
问题 Consider: Class Client Class Project Class Ticket Class Reply Clients have a sub collection of projects, projects have a sub collection of tickets and tickets have a sub collection of replies. var data = ctx.Set<Ticket>().Include(p => p.Client). Select(p => new { Ticket = p, LastReplyDate = p.Replies.Max(q => q.DateCreated)}); Doesn't work. Neither project nor client are loaded when selecting data this way. I know how to make it work. My question is why doesn't it work like this? 回答1: As

Integration Testing Entity Framework code first with in-memory database

痞子三分冷 提交于 2019-12-17 10:48:10
问题 I'd like to run actual integration tests of my EF4.1 repositories against an in-memory database a la ayende's nhibernate version. I have a code first model, against a legacy database (old table and column names need mapping to my entites using code configurations). I'd like to be able to use Sqlite (or other) to: Generate an in-memory database from my model Create a DBContext for my model with this in-memory database I have already in place IoC/DI of a IDBContextFactory which gets constructed

How can set a default value constraint with Entity Framework 6 Code First?

佐手、 提交于 2019-12-17 10:47:38
问题 In a legacy app, most string properties can't be null and need to have a default value of string.empty. I know it's possible to do this with migrations, but I'm looking for a way to do this using the fluent configuration interface: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Properties<string>().Configure(c => { c.HasMaxLength(255); if (!c.ClrPropertyInfo.IsDefined(typeof (NullableAttribute), false)) { c.IsRequired(); // I want to set a default value

How to convert DbSet in Entity framework to ObjectQuery

假装没事ソ 提交于 2019-12-17 09:53:57
问题 I am using CodeFirst approach and struck with an issue where I need to convert DbSet to ObjectQuery. This is what I did for conversion. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>(); where db is the context inheriting from DbContext and Request is class. So, when I try to call the method that expects ObjectQuery as ObjectQueryMethod(objectSet), it throws the following error. "Type of conditional

Mocking or faking DbEntityEntry or creating a new DbEntityEntry

∥☆過路亽.° 提交于 2019-12-17 08:55:58
问题 Following on the heels of my other question about mocking DbContext.Set I've got another question about mocking EF Code First. I now have a method for my update that looks like: if (entity == null) throw new ArgumentNullException("entity"); Context.GetIDbSet<T>().Attach(entity); Context.Entry(entity).State = EntityState.Modified; Context.CommitChanges(); return entity; Context is an interface of my own DbContext. The problem I'm running in to is, how do I handle the Context.Entry(entity)

Mocking or faking DbEntityEntry or creating a new DbEntityEntry

左心房为你撑大大i 提交于 2019-12-17 08:55:15
问题 Following on the heels of my other question about mocking DbContext.Set I've got another question about mocking EF Code First. I now have a method for my update that looks like: if (entity == null) throw new ArgumentNullException("entity"); Context.GetIDbSet<T>().Attach(entity); Context.Entry(entity).State = EntityState.Modified; Context.CommitChanges(); return entity; Context is an interface of my own DbContext. The problem I'm running in to is, how do I handle the Context.Entry(entity)

Entity Framework Code First Using Guid as Identity with another Identity Column

两盒软妹~` 提交于 2019-12-17 08:16:30
问题 a.k.a How can we create multiple identity columns in Code First? Because of clustering performance, a common recommendation is to use an autoincremented integer column instead of a GUID created with newid() . In order to declare a column as autoincrement, you have to specify it with the Annotation [DatabaseGenerated(DatabaseGeneratedOption.Identity)] . But, you can only have one identity in a table. So starting with a base model like: public abstract class ModelBase { // the primary key

How do I define a database view using Entity Framework 4 Code-First?

蹲街弑〆低调 提交于 2019-12-17 07:33:28
问题 How do I define a database view using Entity Framework 4 Code-First? I can't find anything about this anywhere! 回答1: That's because you cannot define database view using code-first approach. Database view is database construct which uses SQL Query on top of existing tables / functions. You can't define such constructs using code first. If you want view you must create it manually by executing CREATE VIEW SQL script for example in custom initializer - it will be similar like this answer. Just