我正在使用ASP.Net MVC4使用SimpleMembership處理成員資格系統的項目。在MVC 4/EF中自定義SimpleMembership與我自己的數據庫4/EF
除了UserProfile表,我應該在哪裏存儲默認數據,我有兩個表在我自己的數據庫(學生,老師)。
我定製了註冊,添加了一個新字段,我要求新用戶定義他是老師還是學生。
public class UsersContext : DbContext
{
//public UsersContext()
// : base("DefaultConnection")
//{
//}
public DbSet<Student> Students { get; set; }
public DbSet<Teacher> Teachers { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserProfile>()
.HasRequired(u => u.Student)
.WithOptional(p => p.UserProfile)
.Map(m => m.MapKey("IDStudent"));
modelBuilder.Entity<UserProfile>()
.HasRequired(u => u.Teacher)
.WithOptional(p => p.UserProfile)
.Map(m => m.MapKey("IDTeacher"));
}
}
和用戶配置文件的表正在成爲這樣
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string AccountType { get; set; }
public virtual Student student { get; set; }
public virtual Teacher teacher { get; set; }
}
我加入這兩個班的學生外鍵,教師
public virtual UserProfile UserProfile { get; set; }
在賬戶控制器/註冊類
WebSecurity.CreateUserAndAccount(
model.UserName,
model.Password,
new { AccountType = model.AccountType },
false);
WebSecurity.Login(model.UserName, model.Password);
UsersContext db = new UsersContext();
var membership = (SimpleMembershipProvider)Membership.Provider;
membership.ValidateUser(model.UserName, model.Password);
membership.ConfirmAccount(model.UserName, model.Password);
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
if (model.AccountType == "Teacher")
{
if (userid != null)
CreateNewTeacher(user.UserId , model.UserName);
}
if (model.AccountType == "Student")
{
if (userid != null)
CreateNewStudentt(user.UserId , model.UserName);
}
寫這兩個類
public void CreateNewStudent(int id, string username)
{
// Attempt to register the user
using (UsersContext db = new UsersContext())
{
Student student = new Student {User_Name = username};
student.id=id;
db.Students.Add(student);
db.SaveChanges();
}
}
public void CreateNewTeacher(int id,string username)
{
using (UsersContext db = new UsersContext())
{
Teacher teacher = new Teacher();
teacher.id=id;
db.Teacher.Add(teacher);
db.SaveChanges();
}
}
我的問題是後: 在我應該建立什麼樣的水平,我自己的表中的用戶(我想保持相同的ID)。 所以,我想知道什麼時候簡單的會員創建用戶在其表中複製它在我的。
如何獲取用戶的ID!
我的會計策略處理好嗎!或者我錯誤地思考這種方式。
UPDATE 我已經添加[關鍵](它尚未產生)
public partial class Agent
{
public Teacher()
{
this.Tasks = new HashSet<Tasks>();
}
[Key]
public int ID_Teacher { get; set; }
public string Name_Teacher { get; set; }
public System.DateTime Bday_Teacher { get; set; }
public string User_Name { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}
public partial class Task
{
[Key, ForeignKey("Teacher"), Column(Order = 0)]
public int ID_Teacher { get; set; }
[Key, ForeignKey("Student"), Column(Order = 1)]
public int ID_Student { get; set; }
[Key, ForeignKey("DateT"), Column(Order = 2)]
public int ID_DateT { get; set; }
[Key]
public Nullable<int> ID_Comment { get; set; }
public virtual Teacher Teacher { get; set; }
public virtual Student Student { get; set; }
public virtual DateT DateT { get; set; }
}
我很欣賞你的時間和精力!
問候,
好像你已經找到了用戶的ID。這似乎更像是[代碼評論](http://codereview.stackexchange.com/)的問題 – Artless
我感謝您的幫助,每當我保存時我都會遇到問題(教師/客戶端)「EntityType'Teacher'沒有定義密鑰。「知道我從SQL Server數據庫生成我自己的數據庫任何建議! – ImnotaGeek
你可以發佈你的'教師'類嗎? – Artless