code-first

EF CodeFirst: Rails-style created and modified columns

拜拜、爱过 提交于 2019-12-04 13:43:53
Using Entity Framework CodeFirst, how do I create a created datetime column that gets populated with the current timestamp everytime a record is inserted for that table, and a modified datetime column that has a timestamp generated evertime a row is updated? Rails does this by default and I was hoping the EF generated database would have this as well, but it doesn't. Is this something that can be done with data annotations? If so, how? Thanks! Ladislav Mrnka It is not supported in EF. EF will not create these columns for you automatically. You must do it yourselves by either: Have Created and

EF code first - composite key

旧巷老猫 提交于 2019-12-04 13:12:18
I have a legacy database which has two collumns and I want to map them as 1 ID is this possible for example public class Product { public string ProductID {get;set;} public string ShortDescription {get;set;} public string UserName {get;set;} } then my Modelbinder looks like this modelBinder.Entity<Product>(). HasKey(p=>p.ProductID) .MapSingle(product => new { colShotDesc = product.ShortDescription, colUser = product.UserName } ).ToTable("Products"); What I need would be something like ProductID = ShortDescription + UserName in the mapping... because these two collumns share a unique key

Node js and Code First

自作多情 提交于 2019-12-04 11:28:55
I've worked on Entity Framework with Code First approach. Now I am learning Node.js I wonder is there a way to make the same code first approach using Node.js and some libraly? I am thinking of using MySql as database. You can look into Sequelize . Example of usage from the home page (I added comments to relate to Code First): var Sequelize = require('sequelize'); // define your "context" var sequelize = new Sequelize('database', 'username', 'password'); // define your "entities" var User = sequelize.define('user', { username: Sequelize.STRING, birthday: Sequelize.DATE }); // use them

Dependency injection in entity classes with code first

你。 提交于 2019-12-04 10:25:45
I'm trying to figure out a way to handle dependency injection in entity framework code first. I stumbled upon an article that uses ObjectStateManager though I don't think it's available in code first, so I'm looking for something similar where I could inject (property injection) an object into a newly created/fetched entity, or maybe there's a different way? I'm using Autofac as the di container You can do DI like this public class YourContext : DbContext { protected ObjectContext ObjectContext { get { return ((IObjectContextAdapter)this).ObjectContext; } } public YourContext(string

Entity Framework CTP5 Code-First: Mapping a class with multiple collections of another class

一个人想着一个人 提交于 2019-12-04 09:12:47
With EF CTP5 Code-First I am trying to map a class model which contains multiple collections in one class pointing to another class. Here is an example of what I mean: public class Company { public int CompanyId { get; set; } public IList<Person> FemaleEmployees { get; set; } public IList<Person> MaleEmployees { get; set; } } public class Person { public int PersonId { get; set; } public Company Company { get; set; } } If I let the database create from this model with a DbContext without further customization, like so: public class MyContext : DbContext { public DbSet<Company> Companies { get;

First try StructureMap and MVC3 via NuGet

三世轮回 提交于 2019-12-04 07:10:26
I'm trying to figure how to config StructureMap for ASP.NET MVC3 I've already using NuGet and I notice that it creates App_Start Folder with a cs file named as StructuremapMVC, so I check it and notice that is the same code but simplified that will be written manually on App_Start section placed on Global.asax... This is my code from IoC Class public static class IoC { public static IContainer Initialize() { ObjectFactory.Initialize(x => { x.Scan(scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); scan.AddAllTypesOf<IController>(); }); x.For<OpcionDB>().Use(() => new

Entity Framework Code First fluent API setting field properties in a for loop

耗尽温柔 提交于 2019-12-04 05:12:25
问题 I am using Entity Framework Code First to create a database table. My model class has ten decimal fields. Currently I am setting the field property like this in the OnModelCreating method: modelBuilder.Entity<Envelopes>().Property(p => p.cell_1_1).HasPrecision(18, 2); Since I have ten fields I am thinking of using a for loop to set this precision property such as the following code: for( int i = 1; i <= 10; i++ ) { modelBuilder.Entity<Envelopes>() .Property(p => p.Equals("cell_1_"+i ))

Entity Framework 4.1 Code First - Define many-to-many using data annotations only

时间秒杀一切 提交于 2019-12-04 03:41:58
Is it possible to define a many-to-many relationship in Entity Framework 4.1 (Code First approach) using Data Annotations only, without model builder? For example, something like: Product = { Id, Name, ... } Category = { Id, Name, ... } ProductCategory = { ProductId, CategoryId } You get the picture. I don't want to have an intermediate entity ProductCategory in the context with two many-to-ones since I don't have any additional data, just the two FKs. Also, I should be able to define table name for the intermediate table for use with an existing database. It is possible to define many-to-many

C# SMO - Scripting Table Data to File. Throwing Error

自闭症网瘾萝莉.ら 提交于 2019-12-04 03:31:43
问题 I'm trying to create a backup of the data contained within my database before I re-create the tables. I've got so far as to connect to the server and create the file to store the SQL. The problem is its throwing an error on the last line. "Object reference not set to an instance of an object" I've been at this project all day so might be missing something that a fresh pair of eyes would help with. Here's the code: public void scriptTables() { string folder = HttpContext.Current.Server.MapPath

Entity Framework Core Code-First: Cascade delete on a many-to-many relationship

时间秒杀一切 提交于 2019-12-04 03:10:21
I'm working on an ASP.NET MVC 6 project with Entity-Framework Core (version "EntityFramework.Core": "7.0.0-rc1-final" ) backed by a SQL Server 2012 express DB. I need to model a many-to-many relationship between a Person entity and an Address entity. As per this guide I modeled it with a PersonAddress join-table entity, because this way I can store some extra info. My goal is to set-up my system this way: If a Person instance is deleted, all the related PersonAddress instances must be deleted. All the Address instances they reference to must be deleted too, only if they are not related to