entity-framework-4.1

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

Implementing one-to-zero-or-one relation in SQL Server

喜欢而已 提交于 2019-12-17 09:45:31
问题 I'm using Entity Framework 4.1 database first approach. I've used legacy database. In my edmx file which created entity classes based on tables in the legacy database, there is a one-to-zero-or-one association between some entities. Although I explored the tables of database and relation between them I didn't find out how one-to-zero-or-one relation have been implemented in database. For more information I put some screenshots of my database diagram and the property of its relation and

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

Entity Framework Code First Date field creation

一个人想着一个人 提交于 2019-12-17 07:11:45
问题 I am using Entity Framework Code First method to create my database table. The following code creates a DATETIME column in the database, but I want to create a DATE column. [DataType(DataType.Date)] [DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] public DateTime ReportDate { get; set; } How can I create a column of type DATE , during table creation? 回答1: The EF6 version of David Roth's answer is as follows: public class DataTypePropertyAttributeConvention :

How to configure many to many relationship using entity framework fluent API

自闭症网瘾萝莉.ら 提交于 2019-12-17 05:07:11
问题 I'm trying to set up a many to many relationship in EF code first but the default conventions is getting it wrong. The following classes describes the relationship: class Product { public int Id { get; set; } public string Name { get; set; } } class Account { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } One Account can have many Products. However the EF conventions will create the DB tables as: Products Table -------

Organizationally, where should I put common queries when using Entity Framework Code First?

删除回忆录丶 提交于 2019-12-17 04:15:00
问题 I'm laying out a new data layer using EF 4.1 Code First, migrating from an older homebrew data layer. I have set up two assemblies, one for my context and one for all the POCO code first classes. I have some business logic, for instance, a query against one table (or a few tables) that is used in several different places. Where should I put it? It can't go in a POCO class because it joins a couple tables and so needs a context. It could go in the context, but that context would become bloated

How do I detach objects in Entity Framework Code First?

泪湿孤枕 提交于 2019-12-17 03:52:33
问题 There is no Detach(object entity) on the DbContext . Do I have the ability to detach objects on EF code first? 回答1: If you want to detach existing object follow @Slauma's advice. If you want to load objects without tracking changes use: var data = context.MyEntities.AsNoTracking().Where(...).ToList(); As mentioned in comment this will not completely detach entities. They are still attached and lazy loading works but entities are not tracked. This should be used for example if you want to load

How to retrieve Data Annotations from code? (programmatically)

天大地大妈咪最大 提交于 2019-12-17 03:34:08
问题 I'm using System.ComponentModel.DataAnnotations to provide validation for my Entity Framework 4.1 project. For example: public class Player { [Required] [MaxLength(30)] [Display(Name = "Player Name")] public string PlayerName { get; set; } [MaxLength(100)] [Display(Name = "Player Description")] public string PlayerDescription{ get; set; } } I need to retrieve the Display.Name annotation value to show it in a message such as The chosen "Player Name" is Frank. ==================================

How to retrieve Data Annotations from code? (programmatically)

吃可爱长大的小学妹 提交于 2019-12-17 03:34:02
问题 I'm using System.ComponentModel.DataAnnotations to provide validation for my Entity Framework 4.1 project. For example: public class Player { [Required] [MaxLength(30)] [Display(Name = "Player Name")] public string PlayerName { get; set; } [MaxLength(100)] [Display(Name = "Player Description")] public string PlayerDescription{ get; set; } } I need to retrieve the Display.Name annotation value to show it in a message such as The chosen "Player Name" is Frank. ==================================

One DbContext per request in ASP.NET MVC (without IOC container)

允我心安 提交于 2019-12-16 22:35:17
问题 Apologies if this has already been answered, but how do you guarantee one Entity Framework DbContext per request if you are not using an IOC container? (The answers I've come across so far deal with IOC container solutions.) It seems like most solutions hook into the HttpContext.Current.Items dictionary, but how do you guarantee disposal of the DbContext when the request is finished? (Or is disposal not absolutely necessary with an EF DbContext ?) Edit I'm currently instantiating and