ef-code-first

How do I enforce an either-or relationship between model properties, using EF Core?

非 Y 不嫁゛ 提交于 2019-12-11 06:25:45
问题 I'm using EF Core. My Customer entity has properties Address1 , Address2 , and AddressFull . Depending on which system sends me the data, I may receive Address1 and Address2 , or I may receive AddressFull . So I need: EITHER Address1 and Address2 required, and AddressFull not-required OR Address1 and Address2 not-required, and AddressFull required So I have: entityTypeBuilder.Property(p => p.Address1).IsRequired(false); entityTypeBuilder.Property(p => p.Address2).IsRequired(false);

If I don't explicitly map a column in code-first EF to an existing DB, will that column still work?

别来无恙 提交于 2019-12-11 06:24:46
问题 I have the following mapping defined: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<InsuranceProvider>() .Map(ins => ins.Properties(p => new { PKID_Insuance = p.Id, InsuranceProvider = p.Name, Address1 = p.Address.Address1, Address2 = p.Address.Address2, City = p.Address.City, State = p.Address.State, Zipcode = p.Address.Zipcode, })).ToTable("Insurance"); } The table and object both share properties such as Phone and Fax with the same name. Do I

Entity framework architecture for WPF MVVM applications

a 夏天 提交于 2019-12-11 05:54:56
问题 I'm currently developing an WPF application sticking to the MVVM-pattern. On the persistence side I'm using the Entity Framework (Code first) with repositories and UnitOfWork: The implementation is described here. I had a lot of good experiences with this architecture in web-context (asp.net MVC). Unfortunately using this architecture on desktop applications doesn't seem to be appropriate. I have some trouble: I'm loading my database model with the Unit Of Work- and Repository-pattern. It's

Incomplete EF code-first cascade delete on many to many relationship

那年仲夏 提交于 2019-12-11 05:51:33
问题 I have a PhoneNumber entity which I'd like to reference across multiple entities. For example, a Contact entity that has many PhoneNumbers, and a Business entity with one PhoneNumber. public class PhoneNumber { [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Number { get; set; } } public class Contact { [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual

Entity Framework 4.1 Code First mapping/populating a single property from two db columns

↘锁芯ラ 提交于 2019-12-11 05:46:56
问题 I know this question has been asked/answered but I can't find it for the life of me. I am creating and ASP.NET MVC 3 app with an existing db but wanted to go code first so I'm used the EF Power Tools CTP1 to get me started. Ultimately, I will refactor to a better solution but to get going I used the MVCScaffolding to gen up controllers and views. I'm struggling with creating a display value ( FullName ) property on my model that is a combination of the FirstName and LastName columns in the DB

How to combine both of code first and database first approaches

 ̄綄美尐妖づ 提交于 2019-12-11 05:22:14
问题 Let's say I'm a new developer in a company. So there is already an existing database for the project. To work on the project, obviously I need to scaffold the existing database(database first approach), which can generate model classes for me to work on. So I start to work on the project and want to add a new column to a table, so I add a new property on the model class then I want to apply this change in the database. So I switch back to code first approach by adding a new migration and

WCF + Entity Framework CodeFirst = DynamicProxies exception?

落爺英雄遲暮 提交于 2019-12-11 05:05:26
问题 I'm trying out EF CodeFirst CTP 5, and using it with WCF Data Services. I'm getting an error that Internal Server Error. The type 'System.Data.Entity.DynamicProxies.Person_C321D7A37002A1B42C3CBAECC27983D77F6B7FCC3F837175B2CBB55CCA66AF55' is not a complex type or an entity type. If I use an edmx-generated Person off the db created by CF, I have no issue. In reading up, it seems that the lazy loading is screwing things up, and previous EF versions have let me turn off proxy generation with an

Automapper map child list with from same type

谁都会走 提交于 2019-12-11 04:58:30
问题 I have entities that may have children and their children may have children and so on... When I get database models all entities are OK with correct children and parent. But the problem comes when I want to map to view model: Is there any possible way to map models from database like this? // database model code first public class Tomato { public int Id { get; set; } public string Name { get; set; } public int? ParentId { get; set; } public virtual Tomato Parent { get; set; } public

Entity Framework code-first, one-to-zero-to-one and one-to-many relationship to same entity

让人想犯罪 __ 提交于 2019-12-11 04:51:53
问题 I'm creating a code-first database with v6.0 of the Entity Framework. I have an Organisation class and a related Location class defined in c# as follows: public class Organisation { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Location> Locations { get; set; } public int? HQLocationId { get; set; } public Location HQLocation { get; set; } } public class Location { public int Id { get; set; } public string Name { get; set; } public int OrganisationId

How do I use MigratorScriptingDecorator in Entity Framework code first?

不羁的心 提交于 2019-12-11 04:51:19
问题 I have problems using the MigratorScriptingDecorator.ScriptUpdate in Entity Framework 4.3.1. When specifying sourceMigration and targetMigration only my initial migration gets written, the rest of my migrations become empty. I have entered a bug report on Microsoft Connect containing reproducing code. https://connect.microsoft.com/VisualStudio/feedback/details/731111/migratorscriptingdecorator-in-entity-framework-migrations-does-not-respect-sourcemigration-and-targetmigration I expect