|
我有休闲的模特
) ~5 O F' W' A. r- ~namespace Prometheus.Models
1 M0 ~ R" l7 C4 N6 _* `- w* O{
j$ h2 R, S' } [Table("eople")]; C1 k* v8 [; `
public class Person : IPerson
" j# C& ^0 N4 W l l; x: {2 C {+ ? O( f3 {7 P# w' M
[Key]
/ `8 T. y/ P9 Y7 G" G2 }+ m [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]% ~" g5 H1 m" N: ~( D3 P; }! U
public int Id { get; set; }: E$ u$ ?" ?" _& a/ h
public string FirstName { get; set; }
9 m7 {; L' T* g2 |3 h- H public string LastName { get; set; }
6 e* ]0 I7 q+ K# r' l4 E7 ] public string Name" f, r( q+ n) p( D% N
{
% \" w7 }" ]* P( ^& x get
p/ I3 q- k5 N4 x% O: ~" Y" ~% { {+ t$ F7 }# c8 n. j% f
return FirstName + " " + LastName;
& A* v' G' q c# d. W8 h }
9 ~- W/ F# f, s; s7 B* \" { set{}& g1 Z& V1 ]8 t9 H( K
}
7 r' L$ X8 u3 |/ Z public string Email { get; set; }( E4 c2 B! ]' S; u3 X4 W1 Y
public DateTime? LastModified { get; set; }
% L- z# Y4 g8 U4 L }
# N3 |/ r/ }4 [2 G$ I} _. ]8 M4 c$ O
一个继承它的人) r7 D, B! n) [+ k
namespace Prometheus.Models8 k6 O! Y4 d; b% O( B1 ^
{
9 z3 o9 C" N" D. O5 k) K- U7 J [Table("UserProfile")]1 i& P7 T9 W( ?# g
public class UserProfile : Person
: j7 S$ K) F# n! P7 J5 I {8 U% ?) u/ C& q) \+ j, P: V. c
public string UserName { get; set; }, K. X6 V# C! C# W. Q# f
public string CNP { get; set; }
& e* A: ~) d* t0 u7 c4 O! o public virtual Faculty Faculty { get; set; }* l7 J- u# }3 h
public bool? IsUSAMV { get; set; }1 @6 s9 L$ r& h6 \. s/ N
public virtual ICollection Results { get; set; }$ H% Q- n9 Q* |! p3 W0 n
public virtual ICollection Projects { get; set; }
3 u3 e& k7 ?, H3 T: {6 d }# f* _: j& S, Q) N3 S
}
, y- t) m2 W" E, X% a1 r和种子的方法
8 p- b- }9 L0 @/ y& @" Kprivate void AddUser(string user, string password,( E) G! N4 b4 X: B* h1 c
SimpleMembershipProvider membership)2 j( }9 R, C+ [ B: Q9 g
{- I$ }2 e8 L% ?/ U+ n: {
if (membership.GetUser(user, false) == null)
" ]! _5 b- j: \% r4 m5 F4 d4 [) u { c& [" W7 X! D% b
WebSecurity.CreateUserAndAccount(user, password, new
1 F4 D; \3 e% h {" O8 q; ^! {4 |. L' u! q
CNP = "1890531111111",- F3 F9 ]; o& f/ q5 L4 b! c
IsUSAMV = true,$ P, _. z v$ Y7 h1 F' }1 I
});, F" W4 ?; R) `% F
}7 c# G/ O# \5 F$ m. q, ?( B
}
5 O7 n! L" t+ t6 A# Q# o当我尝试在没有UserProfile扩展Person的情况下运行种子方法时,一切都很好,但是当它扩展时,我一直收到错误提示。: }# C( @- h, y8 P$ h
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.UserProfile_dbo.People_Id". The conflict occurred in database "rometheusDb", table "dbo.People", column 'Id'.
% _8 c1 _! r9 rThe statement has been terminated.) H2 q& [" X1 `2 d1 R
任何帮助将不胜感激谢谢。
. q( f+ f* E1 O. T6 y2 x2 n& f. i# |2 n9 @- q我尝试将我的功能更新为' v" s* p8 p7 N8 _
private void AddUser(string firstName,string lastName, string password,2 C. h( o7 ^3 _1 {2 q5 ^
SimpleMembershipProvider membership)0 F$ \+ i2 b- X' M) R2 _
{* K2 d: m9 ?- M$ V
var user = firstName + "." + lastName;: s+ J: o0 I+ A4 n- D
if (membership.GetUser(user, false) == null); l P- k. H( l! |: o* Y
{9 b. m l& O4 C7 d! k8 O6 _& _
var profile = new UserProfile()
# m! c0 V7 u' p( A {
* n! T9 z9 b4 y2 l* |* \ ^3 z1 a Email = "test@email.com",
3 [; R6 _0 R* \ FirstName = firstName,8 F, C8 K, `. R
LastName = lastName1 ^9 V c" f5 B) w$ [5 l9 M- j& Y# ]4 _
};
& \" ^: D9 M& l' S' O0 K8 ` _context.Users.Add(profile);
/ X3 Y, X# R& |$ { _context.SaveChanges();1 w: q2 r$ ]4 L
WebSecurity.CreateAccount(user, password);
: P3 B ~, X8 q g }
) A: D' C: Y' N8 ]}
$ \( H- \0 ^5 J, M2 W% f2 b# d& U7 V9 ~但是现在我得到:- 更新条目时发生错误。
: y+ u2 C8 _7 A d2 `: |& W有关详细信息,请参见内部异常。System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,无法为表’UserProfile’中的标识列插入显式值。
x' e/ m/ I0 N) \ ) r/ a. i( e$ H- Y8 h9 K
解决方案: |
|