code-first

Entity Framework Code First with SQL Server Synonyms

流过昼夜 提交于 2019-11-29 02:17:10
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 schema but different data for each company. On logging in, I would dynamically select the database for

Updating a reference to a child object in Entity framework 4.1 (CodeFirst)

戏子无情 提交于 2019-11-29 02:06:33
I'm trying to update an object that I have previously saved with EntityFramework 4.1 (CodeFirst) The class Job has the following properties ... public class Job { [key] public int Id { get; set; } public string Title { get; set; } public Project Project { get; set; } public JobType JobType { get; set; } public string Description { get; set; } } The initial create works fine, but the update only commits changes to the strings.. If I change the child objects eg the JobType Property from JobTypeA to JobTypeB - the change is not committed ... I'm not looking to commit a change to JobType - only to

How do I map TimeSpan with greater than 24 hours to SQL server Code First?

孤人 提交于 2019-11-29 01:28:37
问题 I am trying to map a TimeSpan Code First property to SQL server. Code First seems to be creating it as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours and I need to store longer than 24 hour for event length. What is the best way to handle this with Code First. 回答1: As per my previous question on how to store TimeSpan in SQL I was advised to store it as seconds or tickets etc. In the end I didn't map the TimeSpan column as there is no equivalent in SQL

Can't run code first migrations using migrate.exe

元气小坏坏 提交于 2019-11-28 23:22:54
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 I attempted to declare my connection string manually: Migrate.exe CodeFirst.dll /connectionString="Data

Entity Framework: Tracking changes to FK associations

偶尔善良 提交于 2019-11-28 21:59:16
I'm overriding SaveChanges on my DbContext in order to implement an audit log. Working with many-to-many relationships or independent associations is relatively easy as EF creates ObjectStateEntries for any changes to those kinds of relationships. I am using foreign key associations, and when a relationship between entities changes all you get is an ObjectStateEnty that says for example entity "Title" has "PublisherID" property changed. To a human this is obviously a foreign key in Title entity, but how do I determine this in runtime? Is there a way to translate this change to a "PublisherID"

How to update entities which are modified outside the DbContext?

独自空忆成欢 提交于 2019-11-28 21:35:01
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(specificationToSave); // this works for insert new entities, modified entities will be saved as new entities context

How do i delete single record from table using EF 6.1.1

Deadly 提交于 2019-11-28 21:28:48
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.Remove(stuDetails); _context.SaveChanges(); } There are no changes about how to delete an entity between

EF Code First - Globally set varchar mapping over nvarchar

[亡魂溺海] 提交于 2019-11-28 21:24:37
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 dealing with a VERY large domain model and going through each string field and setting TypeName =

Loading Nested Entities / Collections with Entity Framework

你说的曾经没有我的故事 提交于 2019-11-28 21:21:12
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 virtual long Id { get; set; } public virtual bool IsCurrent { get; set; } public virtual Title Title {

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

走远了吗. 提交于 2019-11-28 20:33:47
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; } public int DebtorCompanyId { get; set; } public int SellerCompanyId { get; set; } public Company