ef-code-first

Is it possible to create User-Defined Data Types when using EF 4.1 Code First?

我是研究僧i 提交于 2019-12-07 00:43:30
When using EF 4.1 Code First, is it possible to create User-Defined Data Types for your schema? Simple answer is no. Longer answer: Current EF implementation leads to multiple issues when trying to use user defined types: The type must be defined prior to its usage in table's DDL definition. Because of that the type cannot be defined in Seed method of database initializer (as often used for other database constructs like triggers or indexes). To make this work you must create whole new initializer by implementing IDatabaseInitializer and separate database creation and table creation because

EF6 code first: How to load DbCompiledModel from EDMX file on startup?

瘦欲@ 提交于 2019-12-06 22:49:50
问题 I want to reduce startup time in EF6 by caching the DbCompiledModel to disk. It's easy to write the EDMX file for a DbContext: EdmxWriter.WriteEdmx(myDbContext, XmlWriter.Create(@"C:\temp\blah.xml")) And it's easy to pass a DbCompiledModel to the DbContext: var db = new DbContext(connectionString, myDbCompiledModel) However there doesn't seem to be any way to read the EDMX file from disk into a DbCompiledModel! How can I do this? NOTE that I have successfully implemented the solution using

Exclude column from being updateable in Entity Framework 4.1 Code First

不羁岁月 提交于 2019-12-06 22:26:20
问题 Does anyone know if we can exclude column from being updated in Entity Framework 4.1 Code First? For example I have 'CreatedOn' field that I don't want to included when doing edit/updates. Is this possible, i.e. selectively excluding field from update operation in EF Code First 4.1? 回答1: If you are working with attached entities you EF will generate updates only for fields which have changed. If you are working with detached entities you must manually say EF what has changed. If you call this

Using Enums with Code First & Entity Framework 5

孤者浪人 提交于 2019-12-06 21:21:27
问题 Just trying to confirm an impression: it seems enums in EF5 + Code First are only supported when declared within the same namespace as the classes using them as property types. Can anyone confirm that? Couldn't find anything on the web for this... 回答1: A relevant bug that was fixed earlier. 4.3 Beta 1 release notes say: Bug fix for GetDatabaseValues . In earlier releases this method would fail if your entity classes and context were in different namespaces. This issue is now fixed and the

Move property to new Entity in Entity Framework Code First Migration

随声附和 提交于 2019-12-06 21:19:26
I'm using Entity Framework Code First approach in my project. I have a problem with database migration. Now I have the entity public class Event { public int Id { get; set; } public string Title { get; set; } public string City { get; set; } } I have already have data in my existing DataBase. Now I want to extend City property, like this public class Event { public int Id { get; set; } public string Title { get; set; } public virtual Location Location { get; set; } } so, City become to Location with many properties. public partial class Location { public int Id { get; set; } public string

How to UPDATE DATABASE in Code First approach automatically after check-in and Publish code in Continuous deployment

耗尽温柔 提交于 2019-12-06 21:04:39
​In our Web API application, Continuous deployment need a following scenario. User will check in code in VS, Code will get automatically build, Code will be Published, Code will be deployed. But If we are using Entity Framework Code First approach, How can we update database without manual commands (Add-Migration/Update Database)and make database up to date with that check-in. You can try to run Add-Migration/Update Database commands in the build/deploy process. Assume you are using vNext build, Add a " Nuget Installer " task in your build definition first to restore the Entity Framework

How to make a repository for class with <T> in EF

时间秒杀一切 提交于 2019-12-06 16:12:25
问题 I have a simple class: public class TestClass<T> { public int ID { get; set; } public T Value { get; set; } } Is is possible to use EF Code First and Repository pattern to save this kind of elements to SQL database? Edit : It seems like the DbSet's cannot be created using the -notation. E.g. public class UnitOFWorkTestContext : DbContext { ... public DbSet<TestClass<int>> TestClass_Int { get; set; } ... } Gives error: The type 'UnitOFWorkTest.Models.TestClass`1[System.Int32]' was not mapped.

EF 6 Code First __MigrationHistory in dbo schema by default

扶醉桌前 提交于 2019-12-06 15:36:31
I am new to Code first Entity framework, when logging into the database after running my app for the first time I got a little confused when I saw the "__MigrationHistory" table. I now understand the need for this table, but do not like it being in the standard dbo schema within the user table, I think its obtrusive and a risk. My first thought was to move it to the system folder. When researching how to achieve this within the EF context all I could find is how to move it from system to dbo. I now get the feeling __MigrationHistory should by default be created within the system folder... is

Insert/Update data to Many to Many Entity Framework . How do I do it?

让人想犯罪 __ 提交于 2019-12-06 14:01:24
问题 My context is => Using this model via Entity Framework code 1st, data table in database becomes => 1) User Table 2) Role Table 3) UserRole Table - A new linked table created automatically Model for User is => Model for Role is => and my O Data query for inserting record for single User/Role table working properly Now, what query should I write, when I want to Insert record to UserRole table can someone have any idea 回答1: // Fetch the user. var user = await metadataManagentClient.For<User>()

Define SQL table primary key as case sensitive using Entity Framework Code First

夙愿已清 提交于 2019-12-06 13:31:08
Is it possible to define a SQL case sensitive primary key in entity framework code first? I know that by default SQL is case insensitive when it comes to strings, just to be clear I don't want to change the entire DB definition to case sensitive, just one table column (primary key). I'm getting data from an external API which sends the data with case sensitive primary keys ('a' and 'A' are different records), I know I can modify them and save them differently in my DB, but this will also require me to be consistent about it everywhere in my code. That's defiantly possible, but I would rather