我有一个父
user
表和一个子userStatus
表,所以很多用户都有一种状态。
用户表有一个子表 personal
并且该表有很多其他条目和更多子表。
当我尝试添加用户时,收到以下错误消息:
MySqlConnector.MySqlException(0x80004005):无法添加或更新 子行:外键约束失败 (
.mediadb
, 约束user
外键 (user_ibfk_1
) 参考FK_UserStatusId
(userstatus
))PK_StatusId
用户表:
{
public user()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "int(11)")]
public int userID { get; set; }
[StringLength(70)]
public string? email { get; set; }
[StringLength(40)]
public string? password { get; set; }
public DateTime? registrationDate { get; set; }
[Column(TypeName = "int(11)")]
public int FK_UserStatusId { get; set; }
[InverseProperty("User")]
public virtual UserStatus? UserStatus { get; set; }
[InverseProperty("users")]
public virtual ICollection<Personal>? personal { get; set; } = new HashSet<Personal>();
}
用户状态表:
public partial class UserStatus
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(TypeName = "int(11)")]
public int PK_StatusId { get; set; }
[StringLength(30)]
public string statusName { get; set; }
[StringLength(400)]
public string? description { get; set; } = string.Empty;
[JsonIgnore]
public virtual ICollection<user>? User { get; set; } = new HashSet<user>();
}
流畅的API:
modelBuilder.Entity<user>(entity =>
{
entity.HasOne(s => s.UserStatus)
.WithMany(g => g.User)
.HasForeignKey(s => s.FK_UserStatusId);
});
数据库是Mysql,当我插入数据时,引用工作正常,用户表引用userStatus(如预期)
根据要求,这是 mysql 日志:
2024-01-26 16:49:45 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2024-01-26 16:49:45 0 [Note] InnoDB: Uses event mutexes
2024-01-26 16:49:45 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2024-01-26 16:49:45 0 [Note] InnoDB: Number of pools: 1
2024-01-26 16:49:45 0 [Note] InnoDB: Using SSE2 crc32 instructions
2024-01-26 16:49:45 0 [Note] InnoDB: Initializing buffer pool, total size = 16M, instances = 1, chunk size = 16M
2024-01-26 16:49:45 0 [Note] InnoDB: Completed initialization of buffer pool
2024-01-26 16:49:45 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2024-01-26 16:49:45 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2024-01-26 16:49:45 0 [Note] InnoDB: Setting file 'F:\xampp\mysql\data\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2024-01-26 16:49:45 0 [Note] InnoDB: File 'F:\xampp\mysql\data\ibtmp1' size is now 12 MB.
2024-01-26 16:49:45 0 [Note] InnoDB: Waiting for purge to start
2024-01-26 16:49:45 0 [Note] InnoDB: 10.4.20 started; log sequence number 58212177; transaction id 72906
2024-01-26 16:49:45 0 [Note] InnoDB: Loading buffer pool(s) from F:\xampp\mysql\data\ib_buffer_pool
2024-01-26 16:49:45 0 [Note] Plugin 'FEEDBACK' is disabled.
2024-01-26 16:49:45 0 [Note] Server socket created on IP: '::'.
2024-01-26 16:49:45 0 [Note] Reading of all Master_info entries succeeded
2024-01-26 16:49:45 0 [Note] Added new Master_info '' to hash table
2024-01-26 16:49:45 0 [Note] F:\xampp\mysql\bin\mysqld.exe: ready for connections.
Version: '10.4.20-MariaDB-log' socket: '' port: 3306 mariadb.org binary distribution
2024-01-26 16:49:45 0 [Note] InnoDB: Buffer pool(s) load completed at 240126 16:49:45
一般日志:
F:\xampp\mysql\bin\mysqld.exe, Version: 10.4.20-MariaDB-log (mariadb.org binary distribution). started with:
TCP Port: 3306, Named Pipe: F:/xampp/mysql/mysql.sock
Time Id Command Argument
240126 16:48:08 8 Connect root@localhost as anonymous on mediadb
8 Query SET NAMES utf8mb4
8 Query SELECT `u`.`userID`, `u`.`FK_UserStatusId`, `u`.`email`, `u`.`password`, `u`.`registrationDate`
FROM `user` AS `u`
WHERE `u`.`email` = '[email protected]'
LIMIT 2
8 Query SET NAMES utf8mb4
8 Query set session transaction isolation level repeatable read
8 Query start transaction
8 Query INSERT INTO `user` (`FK_UserStatusId`, `email`, `password`, `registrationDate`)
VALUES (0, '[email protected]', 'wwwww4444wwww', timestamp('2024-01-26 15:48:08.798646'));
SELECT `userID`
FROM `user`
WHERE ROW_COUNT() = 1 AND `userID` = LAST_INSERT_ID()
8 Query rollback
在 MySQL 和其他 RDBMS 中,如果您有 t1 和 t2,并且 t1.fk 是引用 t2.pk 的外键,那么每当您要在 t1 中插入/更新记录时(在本例中是用户),那么您需要确保 fk(在本例中为 FK_UserStatusId)具有引用表中已存在的值作为主键。
错误消息告诉您您没有以这种方式继续操作。