code-first

Entity Framework database mapping relationships (Duplicate creation using Seed() method)

≡放荡痞女 提交于 2019-11-28 11:22:05
问题 I created a post with an issue and another issue. These can be looked at for references but i consider them as handled. My question arising from these issues and the action i (need or not need) to apply bothers me because i don't quite understand EF its behavior and expectations. I have a Product, PurchasePrice and SalesPrice entity where my initial thought was that 1 Product can have multiple PurchasePrices but that 1 PurchasePrice only can exist in 1 Product (same for SalesPrice). Therefore

How to map a protected property in EF 4.3 code first

我的未来我决定 提交于 2019-11-28 11:11:06
In EF 4.3 documentation says: By default, building a database using Code First does not include private, protected, or internal properties. If you manually included these properties in your model, Code First would ignore any data annotations on those members. This is issue now fixed, and Code First processes the data annotations. My question is how to included a protected property manully with code first, espically using fluent API? Look at http://blog.cincura.net/232147-mapping-private-or-protected-properties-with-code-first-efv4-ctp4/ to the comment from Drew Jones . Not entirely clean, but

Change foreign key constraint naming convention

一世执手 提交于 2019-11-28 11:01:50
We have our own external convention of naming objects and I need to change the naming convention for the auto generated foreign key constraints. Now it looks like: FK_dbo.City_dbo.CityType_City_CityTypeId but I would like it to be called City_FKC_CityType . I found a similar question which says that you can change the name of constraints manually. However, this does not suit me, since I have a lot of tables and foreign key constraints. I found some information about "Custom Code First Conventions" and I am wondering if I can change the name of constraint using this or if there are any methods

Entity Framework Code-First Execute Scalar-Valued Functions

和自甴很熟 提交于 2019-11-28 10:57:30
How can I execute a scalar function using code first? Below is what I have tried but only the query itself is being returned, not the return value. using (var dbContext = new FTTRContext()) { queryResult = dbContext.Database.SqlQuery<string>("SELECT [dbo].[ufnGetTotalUsers] (GETDATE())").ToString(); } SqlQuery returns an instance of DbRawSqlQuery . This class is an enumerable and it expects you to enumerate it via either the standard LINQ operators, or via foreach , etc. .ToString() on this object simply returns the query that will be executed . To get the result you want, use .Single() or

EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context

寵の児 提交于 2019-11-28 09:35:28
While working with EF code first I get error given below at different times: The entity type SomeType is not part of the model for the current context. What are the possible causes of this error? It may occur because: DbContext configured with an incorrect connection string The entity specified is actually not mapped in configuration I got this when my class that inherited from DbContext did not declare the model as a property. For example, I neglected to add a property for FooModel in the code below: public class MyDBContext : DbContext { public DbSet<FooModel> FooModels{ get; set; } // etc.

Returning a DataTable using Entity Framework ExecuteStoreQuery

左心房为你撑大大i 提交于 2019-11-28 09:09:17
I am working with a system that has many stored procedures that need to be displayed. Creating entities for each of my objects is not practical. Is it possible and how would I return a DataTable using ExecuteStoreQuery ? public ObjectResult<DataTable> MethodName(string fileSetName) { using (var dataContext = new DataContext(_connectionString)) { var returnDataTable = ((IObjectContextAdapter)dataContext).ObjectContext.ExecuteStoreQuery<DataTable>("SP_NAME","SP_PARAM"); return returnDataTable; } No, I don't think that'll work - Entity Framework is geared towards returning entities and isn't

EF Code First Readonly column

筅森魡賤 提交于 2019-11-28 08:12:06
I am using EF Code first with database first approach. "with Database.SetInitializer(null);" My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these columns gets data via triggers. Now What I want to do is to make this read only from EF Code first point of view. I.e. I want to be able to see the createddate and ameneded dates from my app but I dont want to amend these data. I have tried using private modifiers on setter but no luck.When I try to add new data to the table it tried to enter DateTime

How to run Seed() method of Configuration class of migrations

痴心易碎 提交于 2019-11-28 08:01:01
I have 2 questions: 1) How can I run Seed() method from the package-manager console without updating-database model? 2) Is there a way how to call Seed() method in the code? Thx for any advice. Answering your first question. Create a Migration by running add-migration SeedOnly Clear out all Up() and Down() code generated if there was any pending changes public partial class SeedOnly : DbMigration { public override void Up() { } public override void Down() { } } Then you can Target a Specific Migration by running update-database -TargetMigration SeedOnly in the Package Manager console After

Deploying database changes with EF 4.1

徘徊边缘 提交于 2019-11-28 07:46:33
Does anyone have any best practices around deploying database changes in an EF 4.1 code-first solution? I know MS does not currently support database migrations for EF 4.1, but obviously people are going to need to do this from time to time. Thanks Once you deployed database to production you must do incremental changes. It means that before you deploy next version you must prepare two databases in your dev box: Database with DB schema currently deployed in production - you should be able to get this from source control so always correctly label / tag your production releases Database with new

Influencing foreign key column naming in EF code first (CTP5)

本小妞迷上赌 提交于 2019-11-28 07:36:34
I have a POCO class that has two one-way unary relationships with another class, both classes share an ancestor. The names of the foreign keys in the generated schema do not reflect the property names. (Properties MainContact and FinancialContact give PersonId and PersonId1 field names). How can I influence schema generation to generate database column names that match the property names? The model looks like this: The code looks like this: public class CustomerContext: DbContext { public DbSet<Organisation> Organisations { get; set; } public DbSet<Person> Persons { get; set; } protected