ef-code-first

How to set on delete cascade for self reference Foreign Key in Entity Framework in ASP.NET

自古美人都是妖i 提交于 2019-12-11 19:28:42
问题 I am developing an ASP.NET MVC project. I am using Entity Framework code first approach to interact with database. But I am having a problem with setting on cascade delete for self-reference foreign key for an entity. This is my entity class with self reference foreign key public class Category { public int Id { get; set; } [Required] [MaxLength(50)] public string Name { get; set; } [MaxLength(55)] public string MmName { get; set; } public int? ParentId { get; set; } [ForeignKey("ParentId")]

How to make two relations between two tables?

佐手、 提交于 2019-12-11 19:17:58
问题 I have an Article that have a creator and a modifier from User table. Here is the model: public class Article { public int ArticleId { get; set; } public string Name { get; set; } public int UserId { get; set; } public int ModifierId { get; set; } public virtual User User { get; set; } public virtual User Modifier { get; set; } } And here is the User model: public class User { public int UserId { get; set; } public string Username { get; set; } public string Password { get; set; } public

Use custom logic in EF Code First migration

狂风中的少年 提交于 2019-12-11 19:06:49
问题 I have an existing Database with a some amount of data (i.e. DB is not empty). Now I made a decision to encrypt some sensitive data. I have a column TemplateBlocks : string . I'm going to replace it with new column TemplateBlocksEnc : byte[] and I did properly changed my Code First model. Then I generated migration and ... stuck! public partial class EncryptTemplateBlocks : DbMigration { public override void Up() { AddColumn("dbo.Devices", "TemplateBlocksEnc", c => c.Binary(nullable: true));

Can I make my Entity Framework DbSet call my table valued function when selecting?

╄→гoц情女王★ 提交于 2019-12-11 18:27:59
问题 I have many existing queries that use DbSet<dbo_Deal> and now have the requirement to filter out confidential deals for unauthorized users. I would like to know if there's a way to override the DbSet<dbo_Deal> so that it selects using a Table Valued parameter instead of its default behavior. I created the following TVF that filters out confidential deals if the user does not have access: CREATE FUNCTION [dbo].[GetDeals](@UserKey int) RETURNS TABLE RETURN ( SELECT d.* FROM dbo.Deal d WHERE d

MVC3/EF4.1 Binding HttpPostedFileBase to model and adding to DB

爷,独闯天下 提交于 2019-12-11 18:22:18
问题 I have an ActionResult which binds data to the model and adds it the the DB. Now what I want, is to have a file uploader, and the ActionResult to store the files and adding their FileName to the DB (so I can show the files/images at a later point). What is the best approach? Here is what I got so far (It can store the files, but im not sure how intelligent EF is, and how complex the datatypes can be): The model class: public class Annonce { public int Id { get; set; } public string Company {

MVC: New row added to Foreign Key table instead of updating related column

醉酒当歌 提交于 2019-12-11 18:09:33
问题 I am newbie in MVC. I have problem about foreign key column. I have two table called Annoucement and Departments. Department_Id is foreign key in Annoucement table. i also checked it database diagram. Here is my models public class Announcement : BaseEntity { public Announcement() { CreatedDatetime = DateTime.Now; } public String Title { set; get; } public string ContentText { set; get; } [Display(Name = "Date")] [DataType(DataType.DateTime), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",

How to achieve cascading dropdown list in generic repository pattern Mvc.Net entity framework?

老子叫甜甜 提交于 2019-12-11 17:28:42
问题 How to create a cascading DropDownList in generic repository pattern using EF Code first, where one registration form in that available country state and city option and this get from different database table public class ManupulationRecord : IHallBook where T : class where I passed "HallProfile" ENtity Model now How achieve City Country State cascading drop down list Adding New Record in View namespace ModelData { [Table("tblHallProfile")] public class HallProfile { [Key] public int

Entity Framework : Code First Approach. Creating Entities using TPT inheritance

China☆狼群 提交于 2019-12-11 17:14:46
问题 I am new to entity framework and I am using code first approach to create entities using TPT inheritance. My requirement is to create the entities as per the attached diagram where ID is PK for Customers table and FK for the AddressDetails and ContactDetails table. Based on the keys I also need to create the association and navigation properties for the entities. Table Diagram In my code I have created entities as public class Customer { [Key] public int Id { get; set; } public string Name {

sql server auditing a table with code first MVC

佐手、 提交于 2019-12-11 16:29:12
问题 So I'm still stuck with the audit table that I've been working on. these are my models: public interface Audit { int AuditById {get;set;} DateTime AuditOn {get;set;} User AuditBy {get;set;} } User { Id {get;set;} Email {get;set;} Password {get;set;} } Profile : Audit { public int Id {get;set;} public string Name {get;set;} public int AuditById {get;set;} public DateTime AuditOn {get;set;} public User AuditBy {get;set;} public User User {get;set;} } this is on my dbase context to make sure

Error selecting from large data table causing time out in EF + code first

守給你的承諾、 提交于 2019-12-11 15:09:24
问题 I am using EF code first model to get the data from data base table in which i have 400,000 records. But when i use the LINQ query something like: var urer = context.UserEntity.Where(c => c.FirstName.Contains('s')); The above statement gives me all the user to whose first name contains 's'. But since this is a huge data base table, it is giving me the following error: An existing connection was forcibly closed by the remote host Please suggest me the best way to do it. I am assigning this