ef-code-first

EF 6.1 Code First - the number of columns specified must match the number of primary key columns

故事扮演 提交于 2019-12-11 13:47:37
问题 I am running into the following error while trying to migrate a database in Entity Framework. The specified association foreign key columns 'question_set_id' are invalid. The number of columns specified must match the number of primary key columns. I dropped the original primary key QuestionSetId and created a composite key relationship. The columns in the composite key relationship also map to foreign keys. I'm not sure what the issue is. Here is the associated entity. public class

It is possible to query a NotMapped property?

拥有回忆 提交于 2019-12-11 13:08:51
问题 i'm working with EF6 code first, and i used this answer to map a List<stirng> in my entitie. This is my class [Key] public string SubRubro { get; set; } [Column] private string SubrubrosAbarcados { get { return ListaEspecifica == null || !ListaEspecifica.Any() ? null : JsonConvert.SerializeObject(ListaEspecifica); } set { if (string.IsNullOrWhiteSpace(value)) ListaEspecifica.Clear(); else ListaEspecifica = JsonConvert.DeserializeObject<List<string>>(value); } } [NotMapped] public List<string>

Code First - Entity Framework - How to expose foreign keys

爱⌒轻易说出口 提交于 2019-12-11 12:47:53
问题 I have the following data object: public class Customer : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Customer> { public Customer () { // this.HasRequired(a => a.EyeColorType).WithMany().HasForeignKey(a => a.EyeColorTypeID); } [Key] public int ID { get; set; } [Required] [MaxLength(100)] public string FirstName { get; set; } [Required] [MaxLength(100)] public string LastName { get; set; } [Required] [ForeignKey("EyeColorTypeID")] public EyeColorType EyeColorType { get; set;

Why isn't my Entity Framework Code First Pluralization working?

妖精的绣舞 提交于 2019-12-11 12:45:55
问题 I'm using Entity Framework Code First. Usually I have no problem, but for a database I'm working with I keep getting errors that it can't find the table in the SQL Server database. Here is what my class looks like: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; public class CustomApp_User { [Key] public int UserID { get; set; } [MaxLength(50)] public string Username { get; set; } [MaxLength(250)] public string Email { get; set; } public DateTime

Entity Framework 4.0 Code-First Dynamic Query

蓝咒 提交于 2019-12-11 12:39:43
问题 I would like to query a table based on a list of KeyValuePair . With a Model-First approach, I could do the following: var context = new DataContext(); var whereClause = new StringBuilder(); var objectParameters = new List<ObjectParameter>(); foreach(KeyValuePair<string, object> pair in queryParameters) { if (whereClause.Length > 0) whereClause.Append(" AND "); whereClause.Append(string.Format("it.[{0}] = @{0}", pair.Key)); parameters.Add(new ObjectParameter(pair.Key, pair.Value)); } var

Entity Framework Table Per Hierarchy Inserting Multiple Id Columns

点点圈 提交于 2019-12-11 12:36:16
问题 I have seriously spent two work days trying to a TPH setup from Database First to Code first. The Error I get is Something like "Invalid Column Name Entity_EntityId/ Entity_Entity_Id1" I've drawn up a very basic reproduction of the issue like so: internal class Program { private static void Main(string[] args) { using (var context = new Context()) { var baseClass = new Base {Name = "Test"}; context.BaseClasses.Add(baseClass); context.SaveChanges(); var baseClasses = context.BaseClasses.ToList

Using Foreign Keys with different Contexts using Entity Framework code-first

雨燕双飞 提交于 2019-12-11 12:35:09
问题 I have a brand-new ASP.NET MVC 5 project with one DbContext using Entity Framework 6.1 and Identity 2.2.1. Trying go use IoC, I created a 2nd DbContext and some classes belonging to that 2nd context. Most of these classes have a foreign key to the IdentityUser class within Identity 2.2.1. The problem I'm having is that when I try to use Add-Migrations I get the following error: One or more validation errors were detected during model generation: {MyProjectName}.DataContexts.IdentityUserLogin:

How to access specific rows and insert values into specific columns of a table?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 12:17:48
问题 I have a basic Model as: public class Events { public int Id { get; set; } public string title { get; set; } public string amount { get; set; } public string Finance_Approval { get; set; } } I want to connect two different controllers to this single view, one for the requesters who can fill only the title and amount fields. And the second controller for Finance Manger to approve the request by filling the Finance_Approval field in the table. I have created two controllers and their razor

EF:CF Many-To-Many in a Detached Context

老子叫甜甜 提交于 2019-12-11 11:48:06
问题 I have two objects with a many to many relationship, let's call them Book and Category. The standard thing to do in Entity Framework - Code First would be to map them together and put a navigation property on either side, which I have done. I am working in a detached context, so I need to serialize my objects. So if I have two Book objects and three Category objects, one of which is shared by the two Book objects, do I suffer a performance loss by serializing the Book objects as they are by

Can I replace some of this with the Fluent Api?

白昼怎懂夜的黑 提交于 2019-12-11 11:42:41
问题 Say I have the following property in a class (where I'm using Code First): [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } I'd rather not decorate my Password property with these annotations. I'd rather use the Fluent Api if possible. How many of these annotations could be done using the Fluent Api? I know Required can be, and