code-first

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

做~自己de王妃 提交于 2019-11-28 01:05:45
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>(); } [Key] public string Name { get; set; } public int Age { get; set; } public virtual ICollection

Error storing Image in SQL CE 4.0 with ASP.NET MVC 3 and Entity Framework 4.1 Code First

这一生的挚爱 提交于 2019-11-27 23:29:57
问题 I'm trying to store/save an image in an SQL Compact Edition (CE) database. I declare the field in my Student model as: [Column(TypeName = "image")] public byte[] Photo { get; set; } The database is created with the image data type for the Photo column as can be seen here: The problem is: When I run the app and try to save a Student with a Photo of 3 MB (for example), I get an exception: validationError.ErrorMessage = "The field Photo must be a string or array type with a maximum length of

Database.SqlQuery calling stored procedure that has multiple output parameters

梦想的初衷 提交于 2019-11-27 21:45:45
问题 I have a stored procedure like below which takes 1 input parameter ( Name ) and returns 2 output parameters( EmployeeId and Salary). Our stored procedure will insert Name into Employee table and give us back EmployeeId and Salary. CREATE PROCEDURE dbo.insertemployee @iName varchar(500), @OEmployeeId int OUTPUT, @OSalary Money OUTPUT We are using EF Code First approach. I am able to insert records into employee table and cannot find how I can access my two output parameters. I know that I need

Create ASP.NET Identity tables using SQL script

孤者浪人 提交于 2019-11-27 21:26:21
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() { Sequence = 0; LastActivity = DateTime.Now; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

Entity Framework auto incrementing field, that isn't the Id

旧城冷巷雨未停 提交于 2019-11-27 20:24:55
I know this isn't the most ideal solution, but I need to add an auto incrementing field to one of my EF Code First objects. This column id NOT the Id, which is a guid. Is there anyway for me to define the auto incrementing field in code, or would creating the column myself and defining in the DB that its auto incrementing work? You can annotate that property with DatabaseGenerated(DatabaseGeneratedOption.Identity) . EF allows only single identity column per table. public class Foo { [Key] public Guid Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Bar { get;

Nullable property to entity field, Entity Framework through Code First

萝らか妹 提交于 2019-11-27 19:34:27
Using the data annotation Required like so: [Required] public int somefield {get; set;} Will set somefield to Not Null in database, How can I set somefield to allow NULLs?, I tried setting it through SQL Server Management Studio but Entity Framework set it back to Not Null . Just omit the [Required] attribute from the string somefield property. This will make it create a NULL able column in the db. To make int types allow NULLs in the database, they must be declared as nullable ints in the model: // an int can never be null, so it will be created as NOT NULL in db public int someintfield { get

MvcMiniProfiler on EF 4.1 Code-First project doesn't profile SQL

。_饼干妹妹 提交于 2019-11-27 18:12:32
I have version 1.6 of the MvcMiniProfiler referenced (via Nuget) and have set everything up as described on the project homepage at http://code.google.com/p/mvc-mini-profiler/ . I have the following code in the Web.config: <system.data> <DbProviderFactories> <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" /> <add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.6.0.0, Culture=neutral, PublicKeyToken

How to remove child one to many related records in EF code first database?

爱⌒轻易说出口 提交于 2019-11-27 17:48:03
Well, I have one-to-many related model: public class Parent { public int Id { get; set; } public string Name { get; set; } public ICollection<Child> Children { get; set; } } public class Child { public int Id { get; set; } public string ChildName { get; set; } } What I want to do is clear Parent.Children and remove related child entities from database. I've already tried: Database context class: modelBuilder.Entity<Parent>() .HasMany(p => p.Children) .WithOptional() .WillCascadeOnDelete(true); this works fine, but I still have redundant records in database with Parent_Id = null fields when I

EF (Entity Framework) 4.3 Migration tool does not work on EF 4.1 DB

无人久伴 提交于 2019-11-27 16:45:34
问题 I want to modify one DB which was developed with EF 4.1 (Code First). I upgraded the project into EF 4.3 and follow this steps: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx Everything is going well, but when I want to test on current DB (EF 4.1 Code First), Update-Database raise this error: Cannot scaffold the next migration because the target database was created with a version of Code First earlier than EF 4.3 and does not contain the

Entity Framework Code First with SQL Server Synonyms

馋奶兔 提交于 2019-11-27 16:33:49
问题 I have the situation where I will have multiple companies accessing a single instance of my application, but I need to segregate their data to avoid accidentally loading data for another company, and to ease per-company database backups. Ideally, I'd like to split the data up into different databases as follows: One SQL Server database for a list of all the users, together with any tables that are common across all users / companies N number of company specific databases , each with the same