entity-framework-4.1

Entity Framework 4.1 Code First Self-Referencing One-to-Many and Many-to-Many Associations

南楼画角 提交于 2019-11-27 10:57:57
问题 I have a User that can have collection of users he likes... Another user can have collection of users he likes.... If User A likes User B and if User B likes User A, then they get to hang out. I need to send each other their contact info. How do we represent such a model in Entity Framework Code First? public class User { public int UserId { get; set; } public int? UserLikeId { get; set; } public virtual UserLike UserLike { get; set; } } public class UserLike { public int UserLikeId { get;

Entity Framework Code-first default data in database

ⅰ亾dé卋堺 提交于 2019-11-27 09:23:54
问题 How do I handle situations in which I need pre-existing data before the app is started or right after the database is generated. For example, I have a list of countries in which I'd like to load into the database after code-first generates it. How do I do this? App is structured as follows: Repository > Service > WebMVC The xml is in the WebMVC project. 回答1: You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways interface. Like: public

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

别说谁变了你拦得住时间么 提交于 2019-11-27 08:54:35
We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys. With the classes below, how would we configure the one-to-many relationship between Report and RunStat such that Report.RunStats would return all of the RunStat entities where the ReportCode fields are equal? public class Report { [Key] public int ReportKey { get; set; } public string Name { get; set; } public int ReportCode { get; set; } // Can we associate

Implementing one-to-zero-or-one relation in SQL Server

淺唱寂寞╮ 提交于 2019-11-27 08:42:28
I'm using Entity Framework 4.1 database first approach. I've used legacy database. In my edmx file which created entity classes based on tables in the legacy database, there is a one-to-zero-or-one association between some entities. Although I explored the tables of database and relation between them I didn't find out how one-to-zero-or-one relation have been implemented in database. For more information I put some screenshots of my database diagram and the property of its relation and correspondent entities in edmx file: The 1-0..1 relation in your database is directly visible. It is built

EF 4.1 loading filtered child collections not working for many-to-many

好久不见. 提交于 2019-11-27 08:06:22
I've been looking at Applying filters when explicitly loading related entities and could not get it to work for a many-to-many relationship. I created a simple model: Brief description: A Student can take many Courses and a Course can have many Students . A Student can make many Presentation , but a Presentation can be made by only one Student . So what we have is a many-to-many relationship between Students and Courses , as well as a one-to-many relationship between Student and Presentations . I've also added one Student , one Course and one Presentation related to each other. Here is the

Enums with EF code-first - standard method to seeding DB and then using?

為{幸葍}努か 提交于 2019-11-27 07:47:22
Is there a standard way to using Enums in EF code-first? There seems to be some examples making use of a wrapper class for the enum. However, I would like to be able to define the enum and have the enum values also seeded into the database using the database initializer. There doesn't seem to be much point in defining the enum and creating a wrapper, if I then have to seed the database table manually from the enum. VinnyG Now supported : http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx The Microsoft Entity Framework June 2011 CTP

Entity 4.1 Updating an existing parent entity with new child Entities

元气小坏坏 提交于 2019-11-27 06:49:32
I have an application where you can create a new type of product and add to that product some ingredients. The product and the ingredients are both entities saved in a database. The product entity has a collection of ingredient entities. (simplified version) public class Product Public Sub New() Me.Ingredients = New List(Of Ingredient)() End Sub Property Ingredients as ICollection(Of Ingredient) end class When I save the product for the first time, all goes well: I just add it to the context and call SaveChanges. myDataContext.Products.Add(product) myDataContext.SaveChanges() Both the product

Entity Framework Code First Case Sensitivity on string PK/FK Relationships

感情迁移 提交于 2019-11-27 06:47:59
问题 I have a fairly simple composite one to many relationship defined using POCO/Fluent API, one column of which is a string. I've discovered that the data in this column in our database is inconsistent in terms of case ie 'abb', 'ABB' - this is our main ERP system and is fed by a variety of sources which are mainly beyond our control. This is leading to problems using EF code first when joining to related tables as the join is silently ignored by EF when the case of PK/FK is different even

EF Code First DBContext and Transactions

五迷三道 提交于 2019-11-27 06:03:55
I would like know what is the best possible way to implement transactions with DBContext . In particular, Does DbContext.SaveChanges implement transaction internall if i change multiple entities? If i want to call DbContext.SaveChanges multiple times(same contxet/different contxets), how transaction can be achieved? Ladislav Mrnka Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { //

entity framework 4.1 invalid column name

…衆ロ難τιáo~ 提交于 2019-11-27 05:47:28
问题 I have two table News and NewsComments. I followed the rules of naming structure NewsComments public class NewsComment : BaseComment { public int NewsId { get; set; } public virtual News News { get; set; } } But query return exception Invalid column name "News_Id". I know what this exception created when in table not related column. CREATE TABLE [dbo].[NewsComments]( [Id] [int] IDENTITY(1,1) NOT NULL, [NewsId] [int] NOT NULL, [Text] [varchar](max) NOT NULL, [UserId] [int] NOT NULL,