entity-framework-5

Entity Framework - Selecting specific columns

廉价感情. 提交于 2019-12-04 19:27:38
I have a successful query that links two tables with a where and orderby clause, but I wanted to add to just select specific columns instead of getting everything back. PART 1 When I attempt this I get syntax errors on the orderby line, if I remove the orderby line the syntax errors move to the where line. Error 3 Cannot implicitly convert type 'System.Linq.IOrderedQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) IQueryable<VendorProfile> query = _db.VendorProfiles .Include("VendorCategories") .Include("VendorsSelected") .Select(s => new { s

MVC4 - Membership - SQL Server Compact 4.0 - Exception: Unable to find the requested .Net Framework Data Provider

岁酱吖の 提交于 2019-12-04 19:24:47
The InitializeDatabaseConnection Method documentation says we can use the MVC 4 "Internet" template's generated membership functionality with SQLCE (SQL Server Compact 4.0 Local Database). I've used this template many times before and have never had a problem when I didn't fiddle with anything and simply used the localDb (the default) that is generated by the Entity Framework the first time we register a user. My requirement is to get the membership tables that are by default generated into 2012 localDb database instead to generate into a SQLCE database. However, I'm getting the following:

EF5, SQL Server, Longitude and Latitude

安稳与你 提交于 2019-12-04 17:39:35
问题 I found that the best type to store lat and long in SQL Server is decimal (9,6) (ref. What datatype to use when storing latitude and longitude data in SQL databases?) and so I did AddColumn("dbo.Table", "Latitude", c => c.Decimal(nullable: false, precision: 9, scale: 6)); AddColumn("dbo.Table", "Longitude", c => c.Decimal(nullable: false, precision: 9, scale: 6)); SQL seems ok, everything is working, BUT when I insert / update a value, i.e. lat = 44.5912853 it is saved like this: 44.590000 I

No constructor with a connection string in Entity Framework datacontext

我是研究僧i 提交于 2019-12-04 17:24:47
问题 I am using Entity Framework 5.0 for my project. I looked on the internet and I saw that for the entity framework data context there was another constructor that had a string parameter for the connection string. On my generated data context I don't have such a constructor. I looked into the base DbContext and it has such a constructor. Was the code generated wrong? I generated the code from a database. Could this be the cause? Turns out that I can edit the code generation template file to add

Ignore Properties in OnModelCreating

怎甘沉沦 提交于 2019-12-04 17:12:35
I'm attempting to use Bounded (Db) Contexts in Entity Framework 5.0, and I'm having problems excluding a property from one of the classes included in a specific context. Here is the information (I'll shorten for brevity) BaseContext.cs public class BaseContext<TContext> : DbContext where TContext : DbContext { static BaseContext() { Database.SetInitializer<TContext>(null); } protected BaseContext() : base("name=Development") { } } IContext.cs public interface IContext : IDisposable { int SaveChanges(); void SetModified(object entity); void SetAdded(object entity); } ISuiteContext.cs public

How do I adapt the Entity Framework POCO T4 template to create classes in a separate project?

旧时模样 提交于 2019-12-04 15:51:32
I like to to keep my code first - or T4 generated - POCOs in a project separated from the DbContext. This helps me ensure my entity classes aren't coupled to any one data access service. When I create a new DB first EDMX model, the wizard generates a T4 template to generate all the POCOs in the same project as the DbContext. How can I modify this template to add classes to a separate project? On closer inspection, it would probably be much easier to move the DbContext to a new project, but the T4 for this has no call to fileManager.StartNewFile so i don't know where to begin telling it to

EntityType 'DbGeography' has no key defined

北慕城南 提交于 2019-12-04 15:22:48
问题 Long time listener, first time caller (finally made an account here!)... I am using Visual Studio 2013 with .NET 4.5.1 and Entity Framework 6 (final releases, not RC or beta). When trying to add a DbGeography property to my entity, I get this error upon execution: One or more validation errors were detected during model generation: Geocoder.DbGeography: : EntityType 'DbGeography' has no key defined. Define the key for this EntityType. DbGeographies: EntityType: EntitySet 'DbGeographies' is

“Unable to create a constant value of type 'User'” when using FirstOrDefault?

白昼怎懂夜的黑 提交于 2019-12-04 14:03:23
I have a method called UpdateOrders that is supposed to allow users to only edit orders that they own, unless the user is an administrator, in which case they can edit all orders. I'm using Entity Framework 5 in .NET 4.5. The following code produces the error "Unable to create a constant value of type 'User'" at the line var target = _context.Orders.FirstOrDefault... . public Order UpdateOrders(Order order) { var userName = HttpContext.Current.User.Identity.Name.ToLower(); var target = _context.Orders.FirstOrDefault(o => o.OrderID == order.OrderID && (o.User.UserName.ToLower() == userName ||

Two One-to-Many relationships in the same table

别说谁变了你拦得住时间么 提交于 2019-12-04 13:10:42
I have a table called SystemAccount , which (up until recently) had a MasterAccountID on it that would point to its parent account (obviously an int? ). My client has now told me that there may be sometimes where an account can have 2 parent accounts (none ever have more than that). I've been trying to make the adjustment in my SystemAccount class, but it's not generating the relationship that I want. Here's part of the class code: [ForeignKey("MasterAccount")] public int? MasterAccountID { get; set; } [ForeignKey("SecondMasterAccount")] public int? SecondMasterAccountID { get; set; } public

Entity Framework 5.0 code-first with MySQL Connector 6.6.5.0 on .Net 4.5

痞子三分冷 提交于 2019-12-04 13:02:19
问题 For the life of me, I can't get my C# WinApp to work with Entity Framework 5.0 with a MySql database using MySql connector 6.6.5.0 ( MySql.Data reference) and MySql Entity 6.5.4.0 ( MySql.Data.Entity reference). Using .Net Framework 4.5 on Visual Studio 2012. At the time of this writing, the versions above are all the latest stable ones. Here's what I have on app.config <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework