code-first

Can't run code first migrations using migrate.exe

瘦欲@ 提交于 2019-11-27 14:55:03
问题 I'm trying to update a database on a test system. When I run update-database in visual studio things work as expected. When I deploy and then try to run on a test machine: Migrate.exe CodeFirst.dll /startupConfigurationFile="..\web.config" I get: no connection string named xxx could be found in the application config file ...even though there is a connection string with that name in the web.config. There is only one .config file, there isn't a config file for the dll that I'm running against

Entity Framework Code First Many to Many Setup For Existing Tables

放肆的年华 提交于 2019-11-27 14:43:35
I have the following tables Essence , EssenseSet , and Essense2EssenceSet Essense2EssenceSet is the linking table that creates the M:M relationship. I've been unable to get the M:M relationship working though in EF code first though. Here's my code: [Table("Essence", Schema = "Com")] public class Essence { public int EssenceID { get; set; } public string Name { get; set; } public int EssenceTypeID { get; set; } public string DescLong { get; set; } public string DescShort { get; set; } public virtual ICollection<EssenceSet> EssenceSets { get; set; } public virtual EssenceType EssenceType { get;

How to update entities which are modified outside the DbContext?

China☆狼群 提交于 2019-11-27 14:11:15
问题 I've a small problem with updating entities if the entity is changed outside the DbContext (is a detached entity). If I attach the modified entity, it's state is not modified. My code looks like this: var specificationToSave = GetSpecificationFromTmpStore(userSessionGuid); using (var context = DataContextFactory.GetDataContext()) { // this works for update, if I change the values inside the context while debugging // but it breaks with new entities context.Specifications.Attach

How do i delete single record from table using EF 6.1.1

我们两清 提交于 2019-11-27 14:04:45
问题 I am using Entity Framework 6.1.1. I am deleting single record from table as following but i am not sure whether its the only way or could further rewrite it in an efficient way. Can someone share comments? Reason : I am asking because many solutions in earlier posts are referring to EF 4.0 and not using the latest version 6.1.1. Guid studentId = student.Id; StudentReportDetail stuDetails = _context.StudentReportDetail.Find(studentId); if (stuDetails != null) { _context.StudentReportDetail

EF Code First - Globally set varchar mapping over nvarchar

给你一囗甜甜゛ 提交于 2019-11-27 13:48:35
问题 I have what should be an easy question but I have been unable to find the answer myself. I am using EF4 CTP-5 Code First Model with hand generated POCOs. It is processing string comparisons in generated SQL as WHERE N'Value' = Object.Property I am aware that I can override this functionality using: [Column(TypeName = "varchar")] public string Property {get;set;} Which fixes the issue for that single occurrence and correctly generates the SQL as: WHERE 'Value' = Object.Property However, I am

Loading Nested Entities / Collections with Entity Framework

瘦欲@ 提交于 2019-11-27 13:45:16
问题 I am trying to Eagerly load all the related entities or collection of Entity in one call. My Entities Looks like: Class Person { public virtual long Id { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } } Class Employee { public virtual long Id { get; set; } public DateTime AppointmentDate { get; set; } public virtual ICollection<EmployeeTitle> Titles { get; set; } public virtual Person Person { get; set; } } Class EmployeeTitle { public

SQL 'time' type in Entity Framework Code First

走远了吗. 提交于 2019-11-27 13:05:35
问题 I'm trying to create a 'time(7)' column in a table with Entity Framework Code First. This is my Entity: public class ShiftDetail { public long Id { get; set; } [Required] public int DayOfWeek { get; set; } [Required] [Column(TypeName="time")] public DateTime StartTime { get; set; } [Required] [Column(TypeName = "time")] public DateTime EndTime { get; set; } public long ShiftId { get; set; } public virtual Shift Shift { get; set; } } As you can see I'm trying to set the database type for the

Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

≡放荡痞女 提交于 2019-11-27 13:01:37
问题 I'm stuck at trying to write the Entity Framework 4.1 code first model for the following DB relationship. Here is a visual of the relationship. dbo.[Companies] can have either Seller or Debtor as Company Types. dbo.[SellerDebtors] defines the connection a Seller Company has with a Debtor Company. The code i've written is based on my original EF 4.0 POCO model code. This is what I've come up with - This code does not work. public class SellerDebtor { public int SellerDebtorId { get; set; }

Howto specify table name with Entity Framework Code First Fluent API

Deadly 提交于 2019-11-27 11:59:44
I have an Entity and I am to configure Entity Framework to map it to a database table with different name. I can easily do this with Code First DataAnnotations ( DataAnnotations.Schema.TableAttribute ). But due to limitations now I have to use Code First Fluent API (my domain objects will be used by external clients, so they shouldn't be technology-specific - e.g. have any references to DataAnnotations) I've searched on MSDN but found nothing. So is it possible and how? Thank you. Martin Cron You'll use the .ToTable() method: modelBuilder.Entity<Department>().ToTable("t_Department"); Source:

How to ignore a property when using Entity Framework Code First [duplicate]

送分小仙女□ 提交于 2019-11-27 11:52:08
问题 This question already has an answer here: Ignoring a class property in Entity Framework 4.1 Code First 2 answers Entity Framework Code First will auto-create a table in the database base based on the Model. Is there an attribute that will avoid this? 回答1: Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property. 回答2: Per the accepted answer and similar question/answer, in addition to [NotMapped] you can also specify it using the Fluent API: protected override