entity-framework-4

EF5 incrementing Unique ID without calling savechanges until user request to save

谁说胖子不能爱 提交于 2019-12-11 12:47:21
问题 How do one add multiple items to a table and child tables based on the parent table unique id in child table without calling save changes every time you add a row, only when the user click on the save button changes must be committed 回答1: It depends on the way how you defined your entities. If you are using independent associations it will simply work. If you are using foreign key associations you will have to use temporary keys for principal entities and set them to foreign key properties in

Entity Framework 4.1 - how to update, insert and delete data in derived classes

烈酒焚心 提交于 2019-12-11 12:19:51
问题 I have a class called UserProfile, that derives from an object called User. How do I insert, update or delete data from the UserProfile? 回答1: You will define DbSet in your context. You can define set of base User type and it will be able to work with User and all derived entity types. public class Context : DbContext { public DbSet<User> Users { get; set; } } And using this is same as any other. Inserting: context.Users.Add(new UserProfile() { ... }); Modifying: var profile = GetSomeProfile()

comparing “string” and “null” in linq to entity

落爺英雄遲暮 提交于 2019-12-11 11:59:03
问题 My code: string name; db.Entities.Where(m=>m.name==(name??m.name)) throw:Should be CHAR, but get NCLOB. //---------------- decimal? id; db.Entities.Where(m=>m.id==(id??m.id)) The code is working. my database is oracle. What should I do to make the code working.? Thanks. 回答1: Beware that in Oracle an empty string is NULL. 回答2: I don't know about Oracle + EF and type mappings. But this is not the way to make predicates optional anyway, because now the part x ?? x.y will always be part of the

Entity Framework 4.0 - Returning List of Model objects + Count of Children Per Object

佐手、 提交于 2019-12-11 11:53:40
问题 I'm running into a common need in my project to return collections of my model objects, plus a count of certain types of children within each, but I don't know if it is possible or how to model a "TotalCount" property in a Model class and populate it as part of on single Entity Framework query, preferably using LINQ queries. Is it possible to do this whilst being able to use the Entity Framework .Include("Object") and .Skip() and .Take()? I'm new to the Entity Framework so I may be missing

Alternative to EntityFunctions.AddSeconds for MySQL

蹲街弑〆低调 提交于 2019-12-11 11:07:16
问题 I need to execute an EF LINQ query that adds some seconds to a datetime against MySQL using the standard Oracle supplied dot net connector. Unfortunately it does not support the EntityFunctions.AddSeconds method. Is there an alternative approach? var result = (from rec in db.Records where EntityFunctions.AddSeconds(rec.SomeDate, rec.SomeSeconds) select rec).FirstOrDefault(); 回答1: DELIMITER $$ drop function IF EXISTS AddSeconds$$ create function AddSeconds(theDate datetime, seconds int)

Better way to update a record using Entity Framework

Deadly 提交于 2019-12-11 10:55:22
问题 I am looking for a more better way to update a record using Entity Framework. Below is what I am using now, where playerFromModel is data fetched from View. public bool UpdatePlayer(PlayerEntity playerFromModel) { bool updateSuccessfull = false; using (var context = new PlayerEntities()) { var player = context.Player.Where(m => m.emailAddress == playerFromModel.EmailAddress).FirstOrDefault(); if (player != null) { player.emailAddress = playerFromModel.EmailAddress; player.first_name =

Which one is faster: Entity Framework vs stored procedures?

蓝咒 提交于 2019-12-11 10:19:56
问题 When it comes down to INSERTing large amount of data (say 100.000 rows) in an ARRAY object, which one do you think it would be faster? Through Entity Framework? (call SaveChanges() when u have inserted 100.000 rows) Looping every row (100.000 times) with an INSERT stored procedure? If you could also provide a reference that would be very awesome. Thanks 回答1: Looping 100K times calling a stored procedure will at a minimum create 100K cross process and/or cross network calls will be slow. If

Invalid column name after mapping

一个人想着一个人 提交于 2019-12-11 09:37:31
问题 I have added a (bit\bool) column IsController to one of my tables ALTER TABLE P_USER ADD IsController bit NOT NULL DEFAULT 0 Updated the edmx and added IsController to the MY_USER entity, then changed its name to IsControllerX and mapped it to the IsController from the table. And set this inside the solution.domain.business cs file: public virtual bool IsControllerX { get; set; } On debug I have the error: An error occurred while executing the command definition. See the inner exception for

Entity framework, abstract class, generic repository and generic manager

旧城冷巷雨未停 提交于 2019-12-11 09:27:51
问题 In a project, we use generic repository and generic manager so we don't need to rewrite every update / delete etc method in every repository / manager. Here is how they looks : public interface IBaseRep<T> : IDisposable where T : class, PrivateObject { DbSet<T> DatabaseSet { get; set; } DbContext Dal { get; set; } T Find(int? id); T Find(Expression<Func<T, bool>> predicate); ICollection<T> Select(Expression<Func<T, bool>> predicate = null, Expression<Func<T, string>> orderBy = null, string

Proper way to return IQueryable using WCF Data Service and EF

ε祈祈猫儿з 提交于 2019-12-11 09:04:27
问题 So I want to return some data, and I'm using WCF Data Services and Entity Framework, that looks like: public class MyWcfDataService : DataService<MyEFModel> { [WebGet(ResponseFormat = WebMessageFormat.Json)] public IQueryable<GetMyListEF> GetMyList() { using (MyEfModel context = this.CurrentDataSource) { return context.GetMyListEF().ToList().AsQueryable(); } } } As you can see I'm casting to a list, then to queryable. If I only cast AsQueryable() , I won't be able to Read the data because the