ef-code-first

How to work with LocalDB and EF, without using migrations

梦想与她 提交于 2019-12-09 05:59:28
问题 I am on VS 2012 RTM, with EF 5. I am doing Code First, but trying to ignore Code Migrations since I am just in development. To avoid them, I have this set Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SomeContext>()); Occasionally, even when I don't think I've touched the model, it decides to recreate. That's fine. But it usually results in the error Cannot drop database because it is currently in use. So I decided to delete the database entirely. I go into VS, go to the "SQL

Entity framework, code first. Child objects not populating when called

自闭症网瘾萝莉.ら 提交于 2019-12-09 05:35:09
问题 I'm getting to grips with EF code first. My domain model design doesn't seem to support the auto 'populating' child of objects when I call them in code. Model: public class Car { [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required,MaxLength(10)] public string Registration { get; set; } [MaxLength(30)] public string Make { get; set; } [MaxLength(45)] public string Model { get; set; } [Required] public Coordinates Coordinates { get; set; } [Required]

How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations?

强颜欢笑 提交于 2019-12-09 05:04:38
问题 I've been searching around the web trying to figure out the right syntax to have Entity Framework Code First create my table with a column: varchar(max). This is what I have. By default this creates varchar(128). How do I create varchar(max)? I have tried [MaxLength] without success. Any help would be appreciated. Thanks! [Column(TypeName = "varchar")] public string MediaDesc { get; set; } 回答1: [Column(TypeName = "varchar(MAX)")] Surprisingly the most obvious solution works. The [MaxLength]

EF 4 Code First - Combine Views and Tables

。_饼干妹妹 提交于 2019-12-09 01:57:47
问题 I researched this question for days and cannot seem to find an option I feel good about; however, here is a link to a very similar question: Add Calculated field to Model Ultimately, I have the same question, but I am hoping for a better solution. Consider the following DB Tables: CREATE TABLE [Contact]( [ContactID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, [ContactName] [varchar](80) NOT NULL, [Email] [varchar](80) NOT NULL, [Title] [varchar](120) NOT NULL, [Address1] [varchar](80)

EF Code First, map two navigation properties to the same object type

浪子不回头ぞ 提交于 2019-12-09 00:39:09
问题 If I have a User class that has these properties: public Guid UserPreferenceId { get; set; } public virtual DefaultUserPreference UserPreference { get; set; } public Guid SecondaryUserPreferenceId { get; set; } public virtual DefaultUserPreference SecondaryUserPreference { get; set; } How can I get this to make via fluent API? When I try to run this it says: Introducing FOREIGN KEY constraint on table 'Users' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE

Entity Framework Code First 4.3 / LINQKit predicate for related table

不羁的心 提交于 2019-12-09 00:06:33
问题 I am using Entity Framework 4.3.1 with a Code First approach. Also, I am using LinqKit so as to use the PredicateBuilder. If I have tables like so: Location, TimeZone (Many:1) ..and I wish to have something like so: Expression<Func<TimeZone, bool>> pred = PredicateBuilder.True<TimeZone>(); pred = pred.And(tz => tz.TimeZoneCode == "EST"); List<Location> locations = context.Locations .AsExpandable().Where(pred) .Select(loc => loc).ToList(); This does not work, because the predicate is built to

Why does Setting EntityState to Detached Empty a Property of type List<T>?

天涯浪子 提交于 2019-12-08 23:48:28
问题 Using Entity Framework Code First, I have something like: public class Foo { public int Id { get; set; } public List<Bar> Bars { get; set; } } Foo foo = (from f in ctx.Foos.Include("Bars") where f.Id == 42 select f).Single(); // At this point foo.Bars is populated ctx.Entry(foo).State = EntityState.Detached; // At this point foo.Bars is an empty List Why does detaching an object cause it's property public List<string> Bars , which was explicitly and successfully included, to be emptied? What

Avoiding Shotgun Surgery with Database.SetInitializer

老子叫甜甜 提交于 2019-12-08 21:48:02
问题 The canonical answer on where to put Database.SetInitializer calls is in Global.asax for web projects. I'm looking for another option. We're using Entity Framework 4.3.1 with Code First. We write web services and WinForms applications, and typically put data access code (such as DbContexts) in shared libraries. Currently, the constructors of our DbContext descendants look like this: public PricingContext(string connectionString) : base(connectionString) { Database.SetInitializer

Domain Modeling Question using C# with Entity Framework CTP 4

和自甴很熟 提交于 2019-12-08 21:31:38
问题 I am trying to model out a album that has a collection of photos. Each Album will have a collection of Photos and a Photo that is a thumb. This is what I have but EF does not seem to like it: public class Album : IEntity { private DateTime _dateCreated; public Album() { _dateCreated = SystemTime.Now(); Photos = new List<Photo>(); } public long Id { get; set; } public string Name { get; set; } public string Location { get; set; } public DateTime DateCreated { get { return _dateCreated; } set {

Unique constraint with EFCodeFirst and SqlCe4

蓝咒 提交于 2019-12-08 20:30:45
问题 I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I have been designing several models, and found one that is turning out to be interesting. This is what I have so far: public class Preference { public int PreferenceId { get; set; } [Required] public int StudentId { get; set; } public virtual Student Student { get; set; } [Required] public int PresentationId { get; set; } public virtual Presentation Presentation { get; set; } [Required] public int Rank {