种子成员与自定义数据实体框架

编程入门 行业动态 更新时间:2024-10-27 22:34:31
本文介绍了种子成员与自定义数据实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有休闲模型

namespace Prometheus.Models { [Table("People")] public class Person : IPerson { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Name { get { return FirstName + " " + LastName; } set{} } public string Email { get; set; } public DateTime? LastModified { get; set; } } }

继承它的人

namespace Prometheus.Models { [Table("UserProfile")] public class UserProfile : Person { public string UserName { get; set; } public string CNP { get; set; } public virtual Faculty Faculty { get; set; } public bool? IsUSAMV { get; set; } public virtual ICollection<Result> Results { get; set; } public virtual ICollection<Project> Projects { get; set; } } }

而种子方法

private void AddUser(string user, string password, SimpleMembershipProvider membership) { if (membership.GetUser(user, false) == null) { WebSecurity.CreateUserAndAccount(user, password, new { CNP = "1890531111111", IsUSAMV = true, }); } }

当我尝试运行没有UserProfile扩展的种子方法人的一切都可以,但是当它延伸时,我会继续收到休眠错误。

When i try to run the seed method without UserProfile extending Person everything is ok, but when it extends it i keep getting the fallowing error.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.UserProfile_dbo.People_Id". The conflict occurred in database "PrometheusDb", table "dbo.People", column 'Id'. The statement has been terminated.

任何帮助都将非常感谢。

Any help would be greatly appreciated thanks.

我尝试将我的功能更新为

I tried updateing my function to

private void AddUser(string firstName,string lastName, string password, SimpleMembershipProvider membership) { var user = firstName + "." + lastName; if (membership.GetUser(user, false) == null) { var profile = new UserProfile() { Email = "test@email", FirstName = firstName, LastName = lastName }; _context.Users.Add(profile); _context.SaveChanges(); WebSecurity.CreateAccount(user, password); } }

但现在我得到: - strong>更新条目时出错。查看内部例外情况。 System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,不能在表'UserProfile'中为identity列插入显式值。

推荐答案

您在创建项目的顺序和数据库的完整性检查时遇到问题。我做了类似的事情,它涉及到先保存用户帐户。我的代码如下:

You are having a problem in the order that items are created and the integrity checks on the db. I have done something similar and it has involved saving the user first then the account. My code looks like:

var user = new User { UserName = model.UserName, Organisation = userOrg }; this.repository.SaveUser(user); string token = WebSecurity.CreateAccount(model.UserName, model.Password, true);

注意使用 CreateAccount 而不是 CreateUserAndAccount 方法

更多推荐

种子成员与自定义数据实体框架

本文发布于:2023-11-12 04:55:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   实体   框架   种子   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!