code-first

Seeding Many-To-Many data

元气小坏坏 提交于 2021-02-09 07:13:25
问题 Hello I'm trying to create some seeds for my project, but I'm having trouble seeding the Many-To-Many relationship data to the db. My database looks like this: in TeacherSkills , Teacher_ID and Skill_ID are foreign keys for their tables ofcourse. My seeder looks like this protected override void Seed(Ability_Examen_ASP.Models.AbilityDbContext context) { if (!context.Skills.Any()) { context.Skills.Add(new Models.Skill { SkillName = "PHP" }); context.Skills.Add(new Models.Skill { SkillName =

EF Code First abstract relationship?

若如初见. 提交于 2021-02-07 18:31:56
问题 I have a class that inherits a base class to which another class has relationships. Example: Base class: Animal Subclass 1: Dog Subclass 2: Cat Related one-to-many table: Vaccinations A dog can have multiple vaccinations. This is implemented as a List<Vaccination>. A cat can have multiple vaccinations. A vaccination record can only have one animal associated with it. A vaccination doesn't know if it's associated with a dog or a cat. (Dogs and cats use non-colliding GUIDs.) There is no Animal

EF Code First abstract relationship?

六眼飞鱼酱① 提交于 2021-02-07 18:31:31
问题 I have a class that inherits a base class to which another class has relationships. Example: Base class: Animal Subclass 1: Dog Subclass 2: Cat Related one-to-many table: Vaccinations A dog can have multiple vaccinations. This is implemented as a List<Vaccination>. A cat can have multiple vaccinations. A vaccination record can only have one animal associated with it. A vaccination doesn't know if it's associated with a dog or a cat. (Dogs and cats use non-colliding GUIDs.) There is no Animal

Entity Framework multiple references to same table

柔情痞子 提交于 2021-02-07 18:30:09
问题 I'm having trouble creating my database with EF code-first. I have an entity Player and an entity friedship. Each friendship references two players. One of the players is the sender, the other one is the receiver of the friendship. This are my entities: Player.cs public class Player { public int PlayerId { get; set; } [Required] public string Name { get; set; } [Required] public string Email { get; set; } [InverseProperty("Receiver")] public virtual List<Friendship> FriendshipsIncoming { get;

EF Code First Update Database Encountered Error “There is already an object named 'Products' in the database”

南笙酒味 提交于 2021-02-07 10:30:17
问题 I'm using EF5 Code First in MVC4. There are tables where fields have decimal datatypes. I just wanted to update the precision of decimal originally from (18,2) to (18,4) . Please help. To achieve the goal I just modified the OnModelCreating something like this: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<DealerLevelDiscount>().Property(d =>d.Discount ).HasPrecision(18, 4); modelBuilder.Entity<DealerTransaction>().Property(d => d.ProductDiscount)

Convert a table with different relational values to excel columns

你离开我真会死。 提交于 2021-02-07 09:47:56
问题 I have these tables: Category CategoryId CategoryTitle ... ICollection<Article> Articles Each category can have several articles: Article ArticleId ArticleTitle NumberOfComment NumberOfView ... ICollection<ArticleReview> Reviews And each article has several reviews by some user: ArticleReview ArticleReviewId ReviewPoint ArticleId ReviewerId i am trying to export excel report using EPPlus package Here is my ExcelExport class : public class excelExport { public string ArticleTitle { get; set; }

"Violation of PRIMARY KEY constraint '…'. Cannot insert duplicate key in object

送分小仙女□ 提交于 2021-01-29 16:30:48
问题 I just started to make my first project with Codefirst-Approach with C#, Linq and MSSQLSERVER and run into an problem when trying to insert a new DB-entry that contains an reference to an already existing element from another table. InnerException {"Violation of PRIMARY KEY constraint 'PK_dbo.Manufacturers'. Cannot insert duplicate key in object 'dbo.Manufacturers'. The duplicate key value is (1d262e43-b9b6-4752-9c79-95d955d460ab).\r\nThe statement has been terminated."} System.Exception

How do I register components at run time with unity DI

萝らか妹 提交于 2021-01-29 08:23:13
问题 I created an Microsoft unity project (my first using Dependency Injection) with my home controller working fine as expected having registered my messaging Class as follows private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<IMessageService, MessageService>(new HierarchicalLifetimeManager()); RegisterTypes(container); return container; } The challenge is I intend making this project a parent to several modules that will be plugins

Best way to control unicity of an attribute using EF Code First

霸气de小男生 提交于 2021-01-29 03:32:12
问题 I've got a class User which has an attribute Name that must be unique. So far I've investigated 3 ways of checking this: Annotations [StringLength(100)] [Index(IsUnique = true)] public string Name { get; set; } Problem is, by trying to insert a user with a repeated name it throws this ex: as you can see, I would have to navigate into the inner exceptions (which I don´t know if it is possible, but I assume it is) and the last inner exception´s message is not user friendly at all. Fluent Api

Best way to control unicity of an attribute using EF Code First

吃可爱长大的小学妹 提交于 2021-01-29 03:24:54
问题 I've got a class User which has an attribute Name that must be unique. So far I've investigated 3 ways of checking this: Annotations [StringLength(100)] [Index(IsUnique = true)] public string Name { get; set; } Problem is, by trying to insert a user with a repeated name it throws this ex: as you can see, I would have to navigate into the inner exceptions (which I don´t know if it is possible, but I assume it is) and the last inner exception´s message is not user friendly at all. Fluent Api