code-first

Can I access the discriminator value in TPH mapping with Entity Framework 4 CTP5

跟風遠走 提交于 2019-11-30 11:49:30
Using Entity Framework 4 CTP5 Code First and this example Is it possible to access the discriminator value? I would like to use it in a projection like context.BillingDetails.Select(x => new { Number = x.Number, DiscrimitatorValue = /* how do I get the discriminator value? */ }); From this post I understand the discriminator cannot be mapped to a property but is there any other way of accessing it? I may be late to the game on this one, but I just added a getter property to the base class that returned the name of the current type: public string DiscriminatorValue { get { return this.GetType()

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

久未见 提交于 2019-11-30 10:52:43
How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs? At present, I have: public class Status { [Required] public int StatusId { get; set; } [Required] [DisplayName("Status")] public string Name { get; set; } } public class Restuarant { public int RestaurantId { get; set; } [Required] public string Name { get; set; } [Required] [EmailAddress] public string Email { get; set; } [Required] public string Telephone { get; set; } [Required] public int StatusId { get; set; } public List<Menu> Menus { get; set; } // NAVIGATION PROPERTIES public virtual Status Status { get;

ALTER TABLE DROP COLUMN failed because one or more objects access this column

老子叫甜甜 提交于 2019-11-30 10:43:20
I am trying to do this: ALTER TABLE CompanyTransactions DROP COLUMN Created But I get this: Msg 5074, Level 16, State 1, Line 2 The object 'DF__CompanyTr__Creat__0CDAE408' is dependent on column 'Created'. Msg 4922, Level 16, State 9, Line 2 ALTER TABLE DROP COLUMN Created failed because one or more objects access this column. This is a code first table. Somehow the migrations have become all messed up and I am trying to manually roll back some changed. I have no idea what this is: DF__CompanyTr__Creat__0CDAE408 You must remove the constraints from the column before removing the column. The

Code-First Reference one-to-many

孤街醉人 提交于 2019-11-30 10:08:19
I have the following two tables: LOCALIZATION Id int Text string DINER Id int Name string Description string Name_LocalizationID int Description_LocationID int Now I want my POCO like this: public class Diner{ public int Id{get;set;} public ICollection<Localization> NameLocalization{get;set;} public ICollection<Localization> DescriptionLocalization{get;set;} } public class Localization{ public int Id{get;set;} public string Text{get;set;} } Question is: How we can map NameLocalization and DescriptionLocalization property to Localization's Id with EF Fluent API? Thanks SQL Server does not

Entity Framework - CTP4 - Code First - How to turn off the automatic pluralization?

好久不见. 提交于 2019-11-30 09:49:23
My entity name is "Contact" and my table name is "Contact". However, the default pluralization support is making EF4 to look for a table named "Contacts". Anybody has any idea on how to turn off the pluralization support? This post has got some details on pluralization support. But still does not give me an answer. I see the following text in this post. First of all, I dont know which physical .tt file I need to make this change. Also, I want to have this feature turned off only for one app and not for all. The code generator in T4 Toolbox has the pluralization turned on by default in Visual

Entity Framework Code First 5 Cascade Delete on many to many tables error

牧云@^-^@ 提交于 2019-11-30 09:38:09
I've this model public class State { public State() { this.Promotions = new List<Promotion>(); this.Branches = new List<Branch>(); this.Stores = new List<Store>(); } public int Id { get; set; } public string Description { get; set; } public virtual ICollection<Promotion> Promotions { get; set; } public virtual ICollection<Store> Stores { get; set; } public virtual ICollection<Branch> Branches { get; set; } } public class Store { public Store() { this.Promotions = new List<Promotion>(); this.Branches = new List<Branch>(); } public int Id { get; set; } public string Name { get; set; } public

SQLite with EF Code First

為{幸葍}努か 提交于 2019-11-30 08:59:16
After my success using SQLite with NHibernate, I am very happy to use it for testing with Entity Framework Code First. If you have some example connections string and set up demos, that would be great and save a bit of time from my hectic day. Thanks a lot. EDIT: Worth mentioning that I am getting this error during debugging when applying crud actions via the EF "data context": Unable to determine the provider name for connection of type 'System.Data.SQLite.SQLiteConnection'. <system.data> <DbProviderFactories> <remove invariant="System.Data.SQLite"/> <add name="SQLite Data Provider" invariant

Code First adding to collections? How to use Code First with repositories?

风格不统一 提交于 2019-11-30 08:14:02
问题 EDIT: This happen only on larger scale projects with repositories. Is there anybody using EF4 with CodeFirst approach and using repositories? Please advise me. Hi. Im currently working with EF4 CodeFirst Classes. In my test project I got two classes, Author and Book (author got books). What I'm trying to do is that I have a AddBook in my Author class, but that wont seem to work like I can't Add it to the collection.. here are my classes and two different exceptions. public class Book { public

Using EF4 Code First: How can I change the Model without losing data

折月煮酒 提交于 2019-11-30 07:14:40
In my Global.asax I have the following line: Database.SetInitializer<myDbSupport> (new DropCreateDatabaseIfModelChanges<myDbSupport>()); If I don't have this, then when i change the model, the sdf database will not have the correct structure. This is ok in a dev environment, but when I move to production and want a DB structure update, i of course, can't afford to drop the table, and lose the data. Is it possible to script DB changes, and run this update before deploying the model with the changed structure? Ladislav Mrnka Initializer is for development. I consider any automatic process

using Guid as PK with EF4 Code First

穿精又带淫゛_ 提交于 2019-11-30 06:20:33
问题 I have this class and table: public class Foo { public Guid Id {get;set;} public string Name {get;set;} } create table Foo ( id uniqueidentifier primary KEY DEFAULT (newsequentialid()), name nvarchar(255) ) the problem is that when i try to save new foo the first one goes with the 0000-000-00 ... id and the second also, so I get constraint exception anybody knows a fix ? 回答1: Have you set Identity StoreGeneratedPattern? You can do it in the OnModelCreating method: modelBuilder.Entity<Foo>()