Understanding ForeignKey attribute in entity framework code first

The required side of the 1..0 relationship MemberDataSet should not have a FK to DeferredData. Instead, DeferredData‘s PK should also be a FK to MemberDataSet (known as shared primary key) public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual DeferredData DeferredData { get; set; } } public class DeferredData … Read more

Code First Migrations and Stored Procedures

I’ve done this like so… In the current migration class – public partial class MyMigration : DbMigration { public override void Up() { … other table creation logic // This command executes the SQL you have written // to create the stored procedures Sql(InstallScript); // or, to alter stored procedures Sql(AlterScript); } public override void … Read more

Adding CreatedDate to an entity using Entity Framework 5 Code First

Here is how I did it: [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime CreatedDate{ get; set; } in my migration’s Up() method: AddColumn(“Agents”, “CreatedDate”, n => n.DateTime(nullable: false, defaultValueSql: “GETUTCDATE()”));

Tool to convert Entity Framework EDMX to Code First

With EF6 Tools & Visual Studio 2013 or Visual Studio 2012, you get the option code first from database (see screenshot below) If you don’t see this option you need to install Entity Framework 6 Tools for Visual Studio 2012 & 2013 http://www.microsoft.com/en-gb/download/details.aspx?id=40762 This doesn’t convert an EDMX to code first (as requested in question), … Read more

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

You can either disable it for your entire context by removing the cascade delete convention in the OnModelCreating method: protected override void OnModelCreating( DbModelBuilder modelBuilder ) { modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); } or, you can do it per relationship using a fluent mapping (also in the OnModelCreating): EDIT: you would put it in your menu entities public class … Read more

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

Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain: Database.SetInitializer(new DropCreateDatabaseAlways<YourContextName>()); Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways: public class MyInitializer : DropCreateDatabaseAlways<YourContextName> { protected override void Seed(MagnateContext context) { // seed database … Read more

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

That answer didn’t work for me (in EF 4.1). To make this work, I added DatabaseGenerated attribute to my key column: public class FacebookUser { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public long FacebookId { get; set; } // … } Hope this helps someone.

SQL Server Express connection string for Entity Framework Code First

The problem with your connection string here is: <add name=”TrempimModel” connectionString=”data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.sdf; User Instance=true” providerName=”System.Data.SqlClient” /> You’re basically defining what “server” you’re connecting to – but you’re not saying what database inside the file to connect to. Also – the file extension for SQL Server Express database files is .mdf (not .sdf – … Read more

using Guid as PK with EF4 Code First

Have you set Identity StoreGeneratedPattern? You can do it in the OnModelCreating method: modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); or using the DataAnnotation attributes: public class Foo { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id {get;set;} public string Name {get;set;} }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)