gpt4 book ai didi

c# - EF Core SQLITE - SQLite 错误 19 : 'UNIQUE constraint failed

转载 作者:行者123 更新时间:2023-11-30 14:21:50 25 4
gpt4 key购买 nike

我正在学习 Entity Framework 核心,将其与 SQLITE 一起使用我在数据库中有 2 个表。

消息:

CREATE TABLE `Messages` (
`Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`IsDeleted` INTEGER NOT NULL,
`FromUserId` INTEGER,
`ToUserId` INTEGER,
`SendDate` TEXT NOT NULL,
`ReadDate` TEXT NOT NULL,
`MessageContent` TEXT,
CONSTRAINT `FK_Messages_Users_ToUserId` FOREIGN KEY(`ToUserId`) REFERENCES `Users`(`Id`) ON DELETE RESTRICT,
CONSTRAINT `FK_Messages_Users_FromUserId` FOREIGN KEY(`FromUserId`) REFERENCES `Users`(`Id`) ON DELETE RESTRICT
);

和用户表:

CREATE TABLE `Users` (
`Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`IsDeleted` INTEGER NOT NULL,
`Name` TEXT,
`DisplayName` TEXT,
`Gender` INTEGER NOT NULL,
`BirthDate` TEXT NOT NULL
);

我的 C# 类看起来像:

public class User
{
public int Id {get;set;}
public bool IsDeleted{get;set;}
[Required]
public string Name{get;set;}

public string DisplayName{get;set;}

[Required]
public char Gender{get;set;}

[Required]
public DateTime BirthDate{get;set;}

public User(string n, string dn, char g, DateTime bd)
{
Name=n; DisplayName = dn; Gender = g; BirthDate = bd;
}

protected User(){}

public override string ToString()
{
return string.Format("{0} ({1}) {2}", this.Name, this.Gender,this.BirthDate.ToShortDateString());
}
}

和消息类:

public class Message 
{
public int Id{get;set;}
public bool IsDeleted{get;set;}
[Required]
public Users.User FromUser{get;set;}

[Required]
public Users.User ToUser{get;set;}

[Required]
public DateTime SendDate{get;set;}
public DateTime? ReadDate{get;set;}

[Required]
public string MessageContent{get;set;}

protected Message(){}

public Message(User from, User to, string content)
{
this.FromUser = from;
this.ToUser = to;
this.MessageContent = content;
this.SendDate = DateTime.Now;
this.ReadDate = DateTime.Now;
}

}

用户表有效,但是当我尝试通过此向消息添加新实体时:

public override int Insert(Message entity)
{
dbc.Messages.Add(entity);
dbc.SaveChanges();
return entity.Id;
}

我收到以下错误:

SQLite Error 19: 'UNIQUE constraint failed

我不知道哪里出了问题。当我手动将数据插入数据库(使用 sqlite 的数据库浏览器)时,它起作用了。类(class)关系还好吗?

我的 dbContextSnapshot 是:

[DbContext(typeof(RandevouDbContext))]
partial class RandevouDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "2.1.4-rtm-31024");

modelBuilder.Entity("RandevouData.Messages.Message", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<int>("FromUserId");

b.Property<bool>("IsDeleted");

b.Property<string>("MessageContent")
.IsRequired();

b.Property<DateTime?>("ReadDate");

b.Property<DateTime>("SendDate");

b.Property<int>("ToUserId");

b.HasKey("Id");

b.HasIndex("FromUserId");

b.HasIndex("ToUserId");

b.ToTable("Messages");
});

modelBuilder.Entity("RandevouData.Users.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<DateTime>("BirthDate");

b.Property<string>("DisplayName");

b.Property<char>("Gender");

b.Property<bool>("IsDeleted");

b.Property<string>("Name")
.IsRequired();

b.HasKey("Id");

b.ToTable("Users");
});

modelBuilder.Entity("RandevouData.Messages.Message", b =>
{
b.HasOne("RandevouData.Users.User", "FromUser")
.WithMany()
.HasForeignKey("FromUserId")
.OnDelete(DeleteBehavior.Cascade);

b.HasOne("RandevouData.Users.User", "ToUser")
.WithMany()
.HasForeignKey("ToUserId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}

顺便说一下。我无法从两侧配置映射。

Message 实体有 UserFrom 和 UserTo 字段,但是用户实体不能有消息,因为一次他是“userFrom”而另一次他可以是“userTo”。

还有一个代码,我在其中创建消息实体

        public int SendMessage(int senderId, int receiverId, string content)
{
var dao = new MessagesDao();
var usersDao = new UsersDao();
var userService = new UserService(mapper);

var sender = usersDao.Get(senderId);
var receiver = usersDao.Get(receiverId);

var entity = new Message(sender,receiver,content);
var id = dao.Insert(entity);
return id;
}

我覆盖 GET 方法的用户 dao

 public override User Get(int id)
{
using (var context = new RandevouDbContext())
{
var user = dbc.Find<User>(id);
return user;
}
}

和我重写 Add 方法的 MessagesDao

 public override int Insert(Message entity)
{
using (var dc = new RandevouDbContext())
{
dc.Messages.Add(entity);
dc.SaveChanges();
return entity.Id;
}
}

顺便说一句,我不知道是否可以,但我有 0 个条目要更新(!?) enter image description here

最佳答案

要修复“UNIQUE”约束异常,您需要确保您正在使用的所有实体都被您要保存到的上下文跟踪。

例如

// Get user from one context

User thisUser = db.Users.Find(3);

//Now imagine you have another context which has been created later on

DbContext newContext = new DbContext();

//and you want to create a message, assign it to your user
//and save it to the new context

Message newMessage = new Message
{
Text = "Hello",
User = thisUser
}

newContext.Messages.Add(newMessage);

// saving this to newContext will fail because newContext is not tracking thisUser,
// Therefore attach it...

newContext.Users.Attach(thisUser).

//now newContext is tracking thisUser *AND* newMessage, so the save operation will work

newContext.SaveChanges();

关于c# - EF Core SQLITE - SQLite 错误 19 : 'UNIQUE constraint failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52718652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com