entity-framework-core

Show Original Values Entity Framework 7

五迷三道 提交于 2019-12-21 19:51:07
问题 I have an audit table that tracks Added, Deleted and Modified. I track this inside Entity Framework instead of using a Database trigger for multiple reasons but really because we use a Process Account and I want to track what user physically made that change to that record. I had this working in with EF 5 & I cannot remember I might have had it working in EF6 as well. Either way I am having the hardest time with EF 7 trying to capture the original values. I noticed that when I am in the watch

Better way to implement a row version with EF Core and MySQL?

China☆狼群 提交于 2019-12-21 17:39:40
问题 If I use the following field in my model: [DatabaseGenerated(DatabaseGeneratedOption.Computed)] [Timestamp] public DateTime RowVersion { get; set; } and then define the column as `RowVersion` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP I get the proper optimistic concurrency behavior from EF. That said, I'm not thrilled about using a timestamp for this since it appears to only be second resolution. And while there isn't a big chance of having 2 clients try to

How to group by week in Entity Framework Core?

佐手、 提交于 2019-12-21 16:15:10
问题 In Entity Framework 6 I can use SqlFunctions.DatePart() method: var byWeek = data.GroupBy(x => SqlFunctions.DatePart("week", x.Date)); But these classes ( DbFunctions and SqlFunctions are not available in Entity Framework Core) (reference). So my question is How can I group by week in Entity Framework core? 回答1: My current workaround for the missing functionality is var firstMondayOfYear = this.GetFirstMondayOfYear(DateTime.Now.Year); var entries = this.entitiesService.FindForLastMonths(this

Entity Framework Core 2.1 throwing ArgumentException on left join

瘦欲@ 提交于 2019-12-21 15:18:14
问题 I recently upgraded my project from .NET Core 2.0 to 2.1. I use Entity Framework Core and have several LINQ expressions that include left joins. The following code is an example: var tickets = (from ticket in dbContext.Tickets join member in dbContext.Members on ticket.MemberID equals member.ID into memberLEFT from member in memberLEFT.DefaultIfEmpty() join memberType in dbContext.MemberTypes on member.MemberTypeID equals memberType.ID into memberTypeLEFT from memberType in memberTypeLEFT

Store files in database using Entity Framework Core

穿精又带淫゛_ 提交于 2019-12-21 12:23:05
问题 How to store files in a SQL Server database using Entity Framework Core (Code-First) in an ASP.NET Core app? I have tried using Filestream but unfortunately I get this error: Cannot call Property for the property 'Avatar' on entity type 'UserProfile' because it is configured as a navigation property. Property can only be used to configure scalar properties Here's the code: public class UserProfile : BaseEntity { public FileStream Avatar { get; set; } public string FirstName { get; set; }

Using a custom attribute in EF7 (core) OnModelCreating

走远了吗. 提交于 2019-12-21 11:05:15
问题 I have a DefaultAttribute defined like so: [AttributeUsage(AttributeTargets.Property)] public class DefaultAttribute : Attribute { /// <summary> /// Specifies this property has a default value upon creation. /// </summary> /// <param name="defaultValue">The default value of the property.</param> /// <param name="useAsLiteral">Set to true if the value is <em>not</em> quoted in the DDL.</param> public DefaultAttribute(object defaultValue, bool useAsLiteral = false) { DefaultValue = defaultValue

Entity Framework Core ignoring .Include(..) without .ToList(..) indirectly

不打扰是莪最后的温柔 提交于 2019-12-21 09:31:45
问题 As noted in "Loading Related Data" from EF Core Documentation we can use .Include(..) to Eagerly Load navigation properties from the DbSet (or generic IQueryable<T> linking back to an EF context). This means that, given the following models: public class TestEntityA { public int Id { get; set; } public int TestEntityBId { get; set; } public TestEntityB TestEntityB { get; set; } public string BProperty { get { return TestEntityB.Property; } } } public class TestEntityB { public int Id { get;

Run migrations on azure for dotnet core site deployed with GitHub

ぐ巨炮叔叔 提交于 2019-12-21 09:10:32
问题 I have a small website built with .Net core that includes a SQLite database and entity framework core. I'm using VS Code and on a mac. It is easy to manage the database locally - dotnet ef database update works great. The problem is running migrations when deploying to Azure. My repo is on GitHub, and I configured Azure to pull code from GitHub when I push to the master branch. The deploy is working fine, but migrations aren't running on Azure. I've seen some suggestions that I can use

Why Linq “where” expression after Select gets evaluated locally when created through a generic method?

一曲冷凌霜 提交于 2019-12-21 05:23:07
问题 I'm implementing Specification pattern with generics and trying to dynamically apply criteria to projected simple (unmapped) versions of mapped entities. In general, it works fine, but Linq evaluates the expression locally as soon as I add Select and apply Where after it. The exact same Linq expression yields correct SQL query, if I build it as a local variable and pass to the same Where . Here's the simplified relevant code snippet: public interface ISomeable { string Some { get; set; } }

Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists

若如初见. 提交于 2019-12-21 05:02:25
问题 I was updating my database by the command line, but then I manually updated one of my tables. This seems to have disrupted my ability to update-database. I receive the following error when I try to update: System.Data.SqlClient.SqlException: There is already an object named 'ClientsAndTestimonials' in the database. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser