entity-framework-core

Using a custom attribute in EF7 (core) OnModelCreating

▼魔方 西西 提交于 2019-12-04 05:16:10
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; UseAsLiteral = useAsLiteral; } public object DefaultValue { get; private set; } /// <summary> ///

aspnet core entity framework 7 self referencing “job” 1 to many table

空扰寡人 提交于 2019-12-04 05:12:52
问题 I have a "Job" table that contains jobs. The fact is Jobs are not always done in one go.. you can have a job that has many visits. I intended to represent that as another job but linked back to the original job via self referencing linkId. I am having trouble representing this using the fluent API. Its a one to many relationship.. one job might have many visits and thus a number of linkId's point back to the orginal job. The link Id would back to the orginal job Id. Its also optional since

Use LinqKit PredicateBuilder for related model (EF Core)

孤街浪徒 提交于 2019-12-04 05:10:05
I want to use LinqKit's PredicateBuilder and pass the predicate into .Any method for related model. So I want to build a predicate: var castCondition = PredicateBuilder.New<CastInfo>(true); if (movies != null && movies.Length > 0) { castCondition = castCondition.And(c => movies.Contains(c.MovieId)); } if (roleType > 0) { castCondition = castCondition.And(c => c.RoleId == roleType); } And then use it to filter model that has relation to model in predicate: IQueryable<Name> result = _context.Name.AsExpandable().Where(n => n.CastInfo.Any(castCondition)); return await result.OrderBy(n => n.Name1)

EF7 beta5: Foreign Key returns null value

耗尽温柔 提交于 2019-12-04 05:04:40
问题 I have created an API using ASP.NET5 and Entity Framework 7.0.0-beta 5. I have the Model, DbContext and the Repository created, and when I try to retrieve data from my database I get all the data, except the foreign key data. The foreign key value is always null. DbContext public class MrBellhopContext : DbContext { public DbSet<Company> Company { get; set; } public DbSet<CompanyStatus> CompanyStatus { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) {

Multi dimensional relationship looses the relation with wrapper

点点圈 提交于 2019-12-04 04:52:32
问题 I think it's something wrong with the model in the example below, but I can't figure out what. In the example I have a Container class with Items containing sub Items. As soon as I try to create a container with more than one level of Items it looses the relationship with the container and therefore fails due to foreign key constraints. The exception I get is: Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details. -

How to translate this SQL query to a LINQ query in EF Core?

不羁岁月 提交于 2019-12-04 04:46:21
问题 I have the following table : Indicators(A INT, B INT, C INT, D INT, TimeInsertedLocal DateTime) . And I have the EF Core mapping entity that maps to this table. I need to translate this SQL query to ef core Linq equivalent query . SELECT A, B, C, D, TimeInsertedLocal FROM Indicators WHERE TimeInsertedLocal >= ( SELECT MAX(I.TimeInsertedLocal) FROM Indicators AS I ) and this is the entity : public class Indicator { public int A { get; set; } public int B { get; set; } public int C { get; set;

EF Core Migrations with Multiple DB Schemas

牧云@^-^@ 提交于 2019-12-04 04:39:53
EF Core 1.1 and SQL Server 2016 We are running a microservice application with some microservices having independent few tables. One of the solutions to have many tables for individual microservice, is to give these services a unique schema (e.g. not DBO) and put all of them in one database (good for cost + maintenance). That works fine in EF 6, however, looking at the generated dbo.__EFMigrationsHistory in core, it looks like core doesn't take the schema into account. I am aware that the migration in EF Core has changed to look in the code rather than the DB, but my problem is the version

Mocking Entity Framework Core context

*爱你&永不变心* 提交于 2019-12-04 04:20:18
I try to test my app so I need to mock my EF context. My code seems to be ok, but I have following exception: "System.ArgumentNullException : Value cannot be null. Parameter name: source" Here is my test method: var options = new DbContextOptionsBuilder<ProductContext>().Options; var settings = new SqlSettings { InMemory = true }; var context = new Mock<ProductContext>(options, settings); var mockTreeService = new TreeService(context.Object); await mockTreeService.CreateTreeAsync("Testing tree", Guid.NewGuid()); context.Verify(x => x.AddAsync(It.IsAny<Tree>(), CancellationToken.None), Times

Custom Execution Strategy Error in VS2017 and Entity framework Core 2.0

会有一股神秘感。 提交于 2019-12-04 03:54:54
问题 I'm reading "Building Web Applications with Visual Studio 2017" (by Philip Japikse, Kevin Grossnicklaus, and Ben Dewey) and am getting stuck. When trying to create a class for a custome execution strategy with Entity Framework Core I get error CR0246 "The type or namespace name 'ExecutionStrategyContext' could not be found (are you missing a using directive or assembly reference?)" The text only states that only System and Microsoft.EntityFrameworkCore.Storage are required references. The EF

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

拟墨画扇 提交于 2019-12-04 03:12:26
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; set; } public string Property { get; set; } } .. then code such as the following should work: context