entity-framework-6

How does one define an (optional) navigation property (?$expand=) in code?

会有一股神秘感。 提交于 2020-01-05 10:29:28
问题 First the explanation and the gist, then the question. So: Let's say I have a view AccountView defined in EF (6.1.1) database first (edmx) so that the code-generated class is //This class is generated from a view by EF (edmx)... public partial class AccountView { public System.Guid Id { get; set; } public int CompanyId { get; set; } } Then I create a partial class in the same namespace ( Entities ) as [MetadataType(typeof(AccounViewMetaData))] public partial class AccounView { //This is added

SUM on navigation property as Linq to SQL

浪尽此生 提交于 2020-01-05 09:09:11
问题 I am trying to find a solution how to execute TotalConversions => Statistics.Sum(sum => sum.Conversions) on database server using Linq to SQL. The problem with current code is that Statistics is ICollection (IEnumerable) and not IQueryable and SUM function fetch all records from database locally and only when SUM the results. This is not solution for us because Statistics contain thousands of records. public class User : Entity { public int Id { get; set; } public virtual ICollection

MVC EF Reverse Engineer Code First primary key column/member missing from views in auto-generated code

寵の児 提交于 2020-01-05 08:59:55
问题 I am using an EF Reverse Engineer Code First project. Specifically, I used the Entity Model, code first from database method that allows one to select only a subset of the database tables for a particular MVC project. The primary key column/member for one of my table models is present in the table model, but when I auto-generated the views from the controller the primary key column/member is missing, except from the Create View. Is this standard behavior for the new version 6 -- Visual Studio

When declaring relationship is not necessary in Entity Framework 6?

喜欢而已 提交于 2020-01-05 08:08:10
问题 In EF6 we have two ways to declare relationship between two tables: Annotations Attributes Fluent API Today (by accident) I removed one relationship between two tables and everything kept working well. I was very surprised because there was no way EF would know how those two tables are connected. Tables look like that: [Table("item")] public class Item { public Item() { ItemNotes = new HashSet<ItemNote>(); } [Key] [Column("itemID", TypeName = "int")] [DatabaseGenerated(DatabaseGeneratedOption

Map custom database value function in Entity Framework

天大地大妈咪最大 提交于 2020-01-05 04:15:12
问题 I have a simple database function, which expects two strings as parameter and returns a string. I want to map this with entity framework. Similar to this question, I've created a simple function header: [DbFunction("dbo", "StripCharacters")] public static string StripCharacters(string input, string pattern = "^A-Z0-9") => throw new NotSupportedException(); Like in the linked post, I get the same error message, as soon as I try to use this method inside one of my queries. The exception message

Mocking Entity Framework 6 ObjectResult with Moq

不打扰是莪最后的温柔 提交于 2020-01-04 18:14:40
问题 How can I mock the Entity Framework 6 ObjectResult with Moq so that I can unit test my code that relies on an EF database connection? Having read numerous questions and answers along these lines, and gleaned many nuggets from what I've read, I've implemented what I feel is a reasonably elegant solution and felt that I should share it, since the community here helped me get there. Thus, I'll proceed to answer this question, and potentially open myself up to some mockery (pun intended): 回答1:

Mocking Entity Framework 6 ObjectResult with Moq

白昼怎懂夜的黑 提交于 2020-01-04 18:13:27
问题 How can I mock the Entity Framework 6 ObjectResult with Moq so that I can unit test my code that relies on an EF database connection? Having read numerous questions and answers along these lines, and gleaned many nuggets from what I've read, I've implemented what I feel is a reasonably elegant solution and felt that I should share it, since the community here helped me get there. Thus, I'll proceed to answer this question, and potentially open myself up to some mockery (pun intended): 回答1:

EF 6 Codefirst -Setting default value for a property defined in base class using fluent API

两盒软妹~` 提交于 2020-01-04 13:36:17
问题 I have a base class which has audit properties like public abstract class BaseModel { [Column(Order = 1)] public long Id { get; set; } public long CreatedBy { get; set; } public DateTime CreatedDate { get; set; } public long ModifiedBy { get; set; } public DateTime ModifiedDate { get; set; } public bool IsActive { get; set; } } All my poco classes derive from this class. I am trying to set a default value to the IsActive properties. I am not keen on using annotations and hence was wandering

LinQ to SQL and where is the choice for all database

孤街浪徒 提交于 2020-01-04 09:29:10
问题 I have the source code like this: var res = from s in Splitting join c in Customer on s.CustomerId equals c.Id where c.Id == customrId && c.CompanyId == companyId select s; When reviewing code, one member said that my code applies for only one SQL db, and advised me to use LinQ to Entity with Join so that it will work for all databases. I don't understand, I think, even with other db, we will add it to Entity Framework. And the code below will work correct too, right? Please advise. 回答1:

Entity Framework Data Migrations for Different Environments

元气小坏坏 提交于 2020-01-04 09:09:10
问题 There are some base data specific for Dev/Test/Prod environments. We are using now Entity Framework Migrations for all the environments but do not know how to specify migrations for particular environments in a way that we specify a migration to be executed only on Dev/Test/Prod. This could be accomplished in Fluent Migrator with Tag attributes. But what about Entity Framework? 回答1: When you say 'base data' I assume you mean Seeding each environment. Migrations provide a seeding mechanism for