code-first

How do I use Entity Framework in Code First Drop-Create mode?

时光怂恿深爱的人放手 提交于 2019-11-27 07:05:19
I'm using Entity Framework v4. I have followed the instructions in the Nerd Dinner tutorial . I'm currently in development mode (not released to any higher environments) and would like for tables to be recreated on each new deployment, since the models are still highly volatile and I don't care to retain data. However, this does not occur. Tables are not created/modified, or anything happening to the DB. If I move to a migrations model by using the Package Manager commands: enable-migrations, add-migration (initial), this works and uses my migrations. However, since I don't yet want to have

Entity Framework Code First Case Sensitivity on string PK/FK Relationships

感情迁移 提交于 2019-11-27 06:47:59
问题 I have a fairly simple composite one to many relationship defined using POCO/Fluent API, one column of which is a string. I've discovered that the data in this column in our database is inconsistent in terms of case ie 'abb', 'ABB' - this is our main ERP system and is fed by a variety of sources which are mainly beyond our control. This is leading to problems using EF code first when joining to related tables as the join is silently ignored by EF when the case of PK/FK is different even

How to map a protected property in EF 4.3 code first

百般思念 提交于 2019-11-27 05:59:49
问题 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? 回答1: Look at http://blog.cincura.net/232147-mapping-private

How to join multiple tables?

北城以北 提交于 2019-11-27 05:53:02
问题 I have the following classes. I have a object var of Description class. I want to select Balance related to the Client provided in the var object using Linq to Sql or Lambda expression. How to join these tables to get the Balance from Account table? public class Description { public int DescriptionID { get; set; } // Attributes public int ClientID { get; set; } [ForeignKey("ClientID")] public virtual Client Client { get; set; } } public class Client { public int ClientID { get; set; } //

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

亡梦爱人 提交于 2019-11-27 05:45:37
问题 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

Entity Framework Code First: how to map multiple self-referencing many-to-many relationships

蹲街弑〆低调 提交于 2019-11-27 04:45:51
问题 I have created an entity type that has multiple collection properties that reference items of the same type. In other words, it reflects a single database table in which the rows are arbitrarily grouped, such that a row may appear in multiple groups. In the following simplified example, the Person class has Brothers and Sisters collection properties that also reference Person entities: public class Person { public Person() { Brothers = new Collection<Person>(); Sisters = new Collection<Person

Cannot get relationship to update for navigation properties in entity framework

╄→гoц情女王★ 提交于 2019-11-27 04:31:55
问题 I am currently using EF4.3 and Code First. Creation of my objects works (via my views - just using the auto-generated Create), but when I attempt to edit an object, it does not save any changes that, utlimately, tie back to my navigation properties. I have been reading on relationships, but I don't understand how to tell my context that the relationship has changed. Here is some example code of my implementation. @* Snippet from my view where I link into my ViewModel. *@ <div class="row">

Create ASP.NET Identity tables using SQL script

岁酱吖の 提交于 2019-11-27 04:31:20
问题 I am attempting to incorporate ASP.NET Identity into a new application that currently uses a SQL script to create the database schema. As we will need to create Foreign Key constraints from other tables to the user tables, it is highly desirable that the ASP.NET Identity tables are also created in the same scripts. I have been able to extend the IdentityUser class in the ApplicationUser class created in IdentityModels.cs- public class ApplicationUser : IdentityUser { public ApplicationUser()

Entity Framework Code-First Execute Scalar-Valued Functions

只愿长相守 提交于 2019-11-27 03:55:02
问题 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(); } 回答1: 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

How do I remove underscore of foreign key fields in code first by convention

你说的曾经没有我的故事 提交于 2019-11-27 03:33:18
I've got multiple classes (including TPT) in my project. Each POCO has a BaseClass , which has a GUID (called GlobalKey ) as primary key. First I used DataAnnotations to create correct foreign keys. But then I've got problems synchronizing the corresponding GUID with the object itself. Now I want to have only one virtual navigation property so that the GUID field in the database is created by NamingConvention . But the field name always adds an underscore followed by the word GlobalKey (which is right). When I want to remove the underscore, I don't want to go thru all my POCOs in the fluent