entity-framework-5

EF Migrations DropForeignKey fails when key is in a base class

坚强是说给别人听的谎言 提交于 2019-12-23 09:34:34
问题 I'm attempting to update my data models for an EF 5.0.0-rc code-first class library. The first command in the Up() method is DropForeignKey("dbo.ChileInventory", new[] { "LotInventoryItemTypeId", "ChileProductId" }, "dbo.ChileProducts"); but I'm getting a SqlException: 'FK_dbo.ChileInventory_dbo.ChileProducts_LotInventoryItemTypeId_ChileProductId' is not a constraint. Could not drop constraint. I think that the cause for the error is due to the fact that my ChileProducts class get's it's key

How to specify a schema for a many-to-many relationship table?

与世无争的帅哥 提交于 2019-12-23 09:30:05
问题 I have a User-Role many-to-many relationship, specified by this excerpt from an EntityTypeConfiguration<Role> derived class that allows me to specifiy schemas for single tables, e.g: [Schema(SchemaNames.ParkPay)] class WeekDayConfig : EntityTypeConfigurationWithSchema<WeekDay> { internal WeekDayConfig() { Ignore(t => t.IsDeleted); Property(p => p.Name) .IsRequired() .HasMaxLength(20); } } Now, for Role , the configuration class contains this code, and the resultant table UserRole gets created

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported Entity Framework 5

你。 提交于 2019-12-23 09:15:37
问题 I have a devexpress GridControl for which I am setting it's datasource like so: var regs = (from vcap in context.chaps select vcap); gridControl1.DataSource = new BindingList<chaps>(regs.ToList()); But when I use the grid, rows I add or delete don't get saved, only changes to the initial rows get saved. If I do this: gridControl1.DataSource = context.chaps.Local; I don't get any rows, and AddNewRow doesn't even add a new row visually. If I do this: gridControl1.DataSource = context.chaps

How to update only modified values (EntityFramework 5.0)?

半世苍凉 提交于 2019-12-23 07:41:55
问题 I have this entity, want to update using entityframework EmployeeModel employee = new EmployeeModel { Id = 1000, //This one must FirstName = modifiedValue, Email = modifiedValue, LastName = originalValue, Phone = originalValue }; Code to update _db.ObjectStateManager.ChangeObjectState(employee, EntityState.Modified); _db.SaveChanges(); This is the SQL statement got once updated Update Employee set Id=1138,FirstName='modifiedValue',Email='modifiedValue',LastName= 'OriginalValue',phone=

Is there a function in Entity Framework that translates to the RANK() function in SQL?

风格不统一 提交于 2019-12-23 07:27:47
问题 Let's say I want to rank my customer database by country. In SQL I would write: select CountryID, CustomerCount = count(*), [Rank] = RANK() over (order by count(*) desc) from Customer Now I want to write this in Entity Framework: var ranks = db.Customers .GroupBy(c => c.CountryID) .OrderByDescending(g => g.Count()) .Select((g, index) => new {CountryID = g.Key, CustomerCount = g.Count, Rank = index+1}); There are two problems with this: It doesn't work. EF throws a System.NotSupportedException

The best overloaded method match for XXX has some invalid arguments

谁说我不能喝 提交于 2019-12-23 05:43:12
问题 I'm a new beginner to the entity framework . I try to write te following method but i get a compile time error. protected static void EntitySQLQuery(AWEntities context) { string cmd = @"SELECT VALUE c FROM AWEntities.Person AS c WHERE c.FirstName = @FirstName"; ObjectQuery<Person> persons = new ObjectQuery<Person>(cmd, context);//Error } The best overloaded method match for 'System.Data.Objects.ObjectQuery.ObjectQuery(string, System.Data.Objects.ObjectContext)' has some invalid arguments 回答1:

Entity Framework Lazy Loading with generic repository

▼魔方 西西 提交于 2019-12-23 03:31:46
问题 My current project uses a generic repository interface, thus: public interface IDataSource : IDisposable { void Add<T>(T newItem) where T : EntityBase; T Get<T>(Guid id) where T : EntityBase; T Get<T>(Expression<Func<T, bool>> predicate) where T : EntityBase; IQueryable<T> GetAll<T>(Expression<Func<T, bool>> predicate = null) where T : EntityBase; int Count<T>(Expression<Func<T, bool>> predicate = null) where T : EntityBase; bool Any<T>(Expression<Func<T, bool>> predicate = null) where T :

Entity Framework Code First Tree Model

為{幸葍}努か 提交于 2019-12-22 20:00:36
问题 I have the following entity: public class Module { public Module() { SubModules = new List<Module>(); } public int Id { get; set; } public string Title { get; set; } public string Action { get; set; } public string Controller { get; set; } public string Icon { get; set; } public List<Module> SubModules { get; set; } } Which, when initialised through Code First generates the following table Schema: CREATE TABLE [dbo].[Modules]( [Id] [int] IDENTITY(1,1) NOT NULL, [Title] [nvarchar](max) NULL,

Get an Entity Object with its child Entity with a condition (using Dynamic Linq)

我的未来我决定 提交于 2019-12-22 11:17:59
问题 This question is continuation of Getting Count() property in Dynamic Lambda Expression I had asked if we can put Count() Method in dynamic lambda expression. I could do it using Dynamic Expression API . Answer provided by - xmojmr But recently I had to implement a Dynamic Lambda Expression to get result with its child entity with filtered data. Details : I have a parent entity with its child entity linked. Now when I get data from parent entity, it returns a collection of data with its child

Mocking DbEntityEntry

删除回忆录丶 提交于 2019-12-22 10:50:04
问题 I am writing unit tests for my Generic Repository layer but i have some problems with DbEntityEntry . My Update method is like below. public virtual void Update(TEntity entityToUpdate) { var entity = dbSet.Find(context.Entry<ITrackedEntity>(entityToUpdate).Entity.Id); context.Entry(entity).CurrentValues.SetValues(entityToUpdate); } As you can see, context.Entry<TEntity>(TEntity entity) method is invoked by caller. So while unit tests this code throws null exception. I had tried to mock