ef-code-first

EF 5.0 Code First Two way navigation withought foreign key id in child

点点圈 提交于 2019-12-12 12:29:10
问题 I have following classes public class Employer { [Key] public Int64 EmployerID { get; set; } public String CompanyName { get; set; } public virtual List<Employee> Employees { get; set; } } public class Employee { [Key] public Int64 EmployeeID { get; set; } public String EmployeeName { get; set; } public virtual Employer EmployerInfo { get; set; } } In the Database context I have set the relation as modelBuilder.Entity<Employer>() .HasMany(p => p.Employees) .WithRequired() .Map(x => x.MapKey(

EF Code First Existing database

…衆ロ難τιáo~ 提交于 2019-12-12 12:26:20
问题 Using EF 4.1 Code first I wanted to stop EF creating database from Models. As I understand if you pass Connectionstring name it will use existing database from that connection string. However if the database does not exists then it will create database will all the tables form the model. <connectionStrings> <add name="DiaryContext" connectionString="Data Source=.;Initial Catalog=Diary;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> public MyDiaryContext() : base("name

EF Core Include() in Many to Many relation

纵饮孤独 提交于 2019-12-12 12:19:29
问题 The relation between Product and Customer is of type many-to-many (from a design point a view). Using EF Core, we split this relation in two one-to-many relations with a third entity: ProductCustomer public partial class ProductCustomer { public long ProductId { get; set; } public long CustomerId { get; set; } public virtual Customer Customer { get; set; } public virtual Product Product { get; set; } public virtual ICollection<UsageRecord> UsageRecord { get; set; } } UsageRecord is a list of

Entity Framework 5: Code-First Cyclical Relationship Issues

天涯浪子 提交于 2019-12-12 11:23:56
问题 I understand why EF does not allow "cyclical references" in the PK/FK relationships. I am looking for advice on how to alter my model to make the following scenario work. Scenario Three entities: Employee , Agency , WorkRecord . Their purpose is to log Employee time spent doing work. Employee then contains reference to the Agency he/she is employed by, and his/her WorkRecord contain reference to the Agency the work was done for. public class Employee { [Key] public int Id { get; set; } public

How can I generate DDL scripts from Entity Framework 4.3 Code-First Model?

戏子无情 提交于 2019-12-12 11:19:50
问题 I have a project I'm trying to deploy and I'm using a cheap host to get started. As part of the hosting package I have a SQL Server database, but I don't have drop or create privileges, I can only use the database they've created for me. Being that's the case, I want to get the DDL so I can run it manually. I'm aware I could script the database create scripts from SQL Management Studio, and that may ultimately work fine, but I'd like to automate the process as much as possible, so if I could

Entity Framework 6 Creating Two table from the same entity object

空扰寡人 提交于 2019-12-12 11:14:59
问题 I was wondering if it's possible to create two table instance from one defined entity object class. Example: public class EntityA() { public String name {get; set;} public String value {get; set;} } public class MyDbConext : DbContext { public DbSet<EntityA> instance1{ get; set; } public DbSet<EntityA> instance2{ get; set; } } What i'm trying to do is create two instances of Entity A with different table names. Is that possible with code first entity framework? I feel like it's seems tedious

Can I have 2 different EntityFramework contexts share a single SqlServer Compact database?

梦想的初衷 提交于 2019-12-12 10:57:52
问题 I have a SqlServer Compact Edition database that contains 2 tables. For each of these tables, I have a context class derived from DbContext which is used by the application to access the table. In order to keep the different components of the application decoupled, I can't have one context class that would have DbSet properties for both of the tables. Rather, I need to have 2 different context class, each of them has to be completely unaware of the other and its data. I'm using the code-first

DbMigrator.GetPendingMigrations() always empty

蹲街弑〆低调 提交于 2019-12-12 10:55:21
问题 I'm using the DbMigrator class to get a list of pending migrations. For some reason it returns no items even though there are pending migrations. Am i missing a step? var configuration = new Migrations.Configuration(); configuration.TargetDatabase = new DbConnectionInfo("MyDatabase"); var migrator = new DbMigrator(configuration); var migs = migrator.GetPendingMigrations().ToList(); Console.WriteLine(migrator.GetPendingMigrations().ToString()); I thought it might be the connection string but

How to Change the Schema name

≯℡__Kan透↙ 提交于 2019-12-12 10:39:54
问题 I´m developing an ASP.NET MVC app that uses EF 4.1 Code First. I have to change the default schema name (dbo) to another name. I tried this: public string SchemaName; public void MyContext() { SchemaName = GetSchemaName(); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Account>().ToTable("TB_ACC_HOLDERS", SchemaName); } But it´s not working. When I get a new instance of my Context and call some of my Tables.. the generated query still with "dbo"

EF 4.1 CodeFirst with Guid (not generated by DB/Store) as PK

痴心易碎 提交于 2019-12-12 10:38:58
问题 I'd like to create a model with a 'client generated' GUID as the primary key. I want the Guid to be auto-generated, but NOT on the DB, so I know the ID before I persist it, important for my domain model. How do I setup: class MyEntity { public Guid ID { get; set; } } So I can do... var newEntity = new MyEntity(); transmitIDBeforePersisting(newEntity.ID); context.MyEntities.Add(newEntity); context.SaveChanges() How can I setup MyEntity.ID so it is auto-generated when I call new, but still