code-first

Entity Framework 4.1 Code First approach to create many-to-many relation

旧时模样 提交于 2019-12-06 21:32:29
问题 I'm using the Silverlight 5 Beta SDK and the EntityFramework 4.1 in an Silverlight Application. I'll try to create the two tables 'Author' and 'Book'. In SQL, there should be a third (join) table, which makes the many-to-many relation between Author and Book (one author could have written many books and a book could be written from many authors). This is what I've got so far: namespace CodeFirst.Models.Web { public class Author { public Author() { this.Books = new HashSet<Book>(); }

EF One-to-many Foreign Keys without child navigation properties

微笑、不失礼 提交于 2019-12-06 19:12:36
问题 Using code-first Entity Framework and .NET 4, I'm trying to create a one-to-many relationship between parents to children: public class Parent { [Key] public int ParentId { get; set; } [Required] public string ParentName { get; set; } public IEnumerable<Child> Children { get; set; } } public class Child { [Key] public int ChildId { get; set; } [ForeignKey] public int ParentId { get; set; } [Required] public string ChildName { get; set; } } As pointed out here, in order for foreign key

Pluggable Conventions in Entity Framework

烂漫一生 提交于 2019-12-06 12:57:38
I am following EF Feature CTP5: Pluggable Conventions to create custom pluggable convention (in this particular case to change the precision of all decimal fields). Looks like in the latest release of EF the Add method on ConventionsConfiguraions is also "internal'. How do i add custom Pluggable Conventions now? The feature has been removed in EF 4.1 and a possible implementation postponed to a later release: Code First customizable (pluggable) conventions are not supported. Removing the default Code First conventions is supported. Quote from here: http://msdn.microsoft.com/en-us/library

How to configure multiple Foreignkey referancing to same table from main table using EF Code First Fluient API

怎甘沉沦 提交于 2019-12-06 09:56:28
问题 I have to design a web application using existing database of desktop application. As per existing database i have below class public class Company { #region Primitive Properties public virtual int CompanyID { get; set; } public virtual string CompanyName { get; set; } public virtual bool IsCustomer { get; set; } public virtual string CustomerCode { get; set; } public virtual bool IsPotentialCustomer { get; set; } public virtual bool IsSupplier { get; set; } public virtual string SupplierCode

Keyword not supported: 'data source'.: EF code-first using ObjectContext and LocalDB

旧时模样 提交于 2019-12-06 09:11:16
问题 I am getting a "keyword not supported error" when I try to connect to a LocalDB database using ObjectContext. This is my connection string: <add name="connStr" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=proj1db;Integrated Security=True" /> and this is the code that tries to create an instance of the ObjectContext: var connectionString = ConfigurationManager .ConnectionStrings["connStr"] .ConnectionString; ObjectContext _context = new

EF code first - composite key

岁酱吖の 提交于 2019-12-06 07:23:52
问题 I have a legacy database which has two collumns and I want to map them as 1 ID is this possible for example public class Product { public string ProductID {get;set;} public string ShortDescription {get;set;} public string UserName {get;set;} } then my Modelbinder looks like this modelBinder.Entity<Product>(). HasKey(p=>p.ProductID) .MapSingle(product => new { colShotDesc = product.ShortDescription, colUser = product.UserName } ).ToTable("Products"); What I need would be something like

EF CodeFirst: Rails-style created and modified columns

核能气质少年 提交于 2019-12-06 07:03:29
问题 Using Entity Framework CodeFirst, how do I create a created datetime column that gets populated with the current timestamp everytime a record is inserted for that table, and a modified datetime column that has a timestamp generated evertime a row is updated? Rails does this by default and I was hoping the EF generated database would have this as well, but it doesn't. Is this something that can be done with data annotations? If so, how? Thanks! 回答1: It is not supported in EF. EF will not

Entity Framework 4 (CTP 5) unwisely queries in comparison with LINQ-to-SQL

倖福魔咒の 提交于 2019-12-06 06:35:07
问题 I have an issue about Entity Framework 4 CTP 5, which I realize LINQ to SQL handle it better than this, but I insist on using EF 4 because of its Code-First feature. So here is my problem: Imagine products with its Tags (or whatever with one to many relation). And tblTags will be changing by deleting some and inserting some tags at a time by user (and user might change them completely different). So in my opinion we can simply delete all old tags and insert new ones (simplest thing to do),

Dependency injection in entity classes with code first

自古美人都是妖i 提交于 2019-12-06 05:38:35
问题 I'm trying to figure out a way to handle dependency injection in entity framework code first. I stumbled upon an article that uses ObjectStateManager though I don't think it's available in code first, so I'm looking for something similar where I could inject (property injection) an object into a newly created/fetched entity, or maybe there's a different way? I'm using Autofac as the di container 回答1: You can do DI like this public class YourContext : DbContext { protected ObjectContext

WCF Ria Services Wizard not working with CodeFirst

本秂侑毒 提交于 2019-12-06 04:14:22
When using EF Code First, It appears WCF RIA Services Wizard (that runs when adding a new DomainService) in VS 2012 does not recognize the DbContexts defined in the project (it only recognizes the ObjectContext). This means that when Adding a New Item in VS2012 and choosing Add DomainServiceClass, the wizard is not able to detect available context classes * combo list is empty * Please see the attached image Any idea what could be happening here? Thanks, -Sumit This is a known issue with Visual Studio 2012, WCF RIA Services and Entity Framework, cause the Wizard to create new Domain Services