Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

order和detail表,如何设置detail的联合主键和自动增长 #23

Closed
zhusheping opened this issue Mar 26, 2019 · 27 comments
Closed

Comments

@zhusheping
Copy link

数据库为Sqlite
1、比如order有Id整形主键,detail表有OrderId 和 Id,如何设置OrderId + Id联合主键

2、实体对象如何定义才会生成表外键。按照导航属性设置后,fsql.CodeFirst.SyncStructure,数据库里面没有外键。

@2881099
Copy link
Collaborator

2881099 commented Mar 26, 2019

[Column(IsPrimary = true]
public int Id { get; set; }

[Column(IsPrimary = true]
public int OrderID { get; set; }

这样应该可以,或者FluentApi方式配置,参考语法

fsql.CodeFirst
.ConfigEntity(a => {
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
})
.ConfigEntity(a => {
a.Name("xxdkdkdk2").SelectFilter("a.Idx > 0");
a.Property(b => b.Id).Name("Id22").IsIdentity(true);
a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
});

@zhusheping
Copy link
Author

谢谢。不过好像不能联合主键自增。如果OrderID是新的,Id也从1开始。

@2881099
Copy link
Collaborator

2881099 commented Mar 26, 2019

确实不可以

@zhusheping
Copy link
Author

改用sqlserver,提示无法将多个 PRIMARY KEY 约束添加到表 'dbo.xxxx'。实体定义和数据库选择相关吗?sqlserver如何定义联合主键?

@2881099
Copy link
Collaborator

2881099 commented Mar 27, 2019

不相关的,可能是数据存在有重复。

或者通过FreeSqlBuilder 创建的时候,.UseCommandMonitor,打印命令,看看执行的迁移语句,在这个贴发出来。

@zhusheping
Copy link
Author

use [Test];
CREATE TABLE [dbo].[ConnModel] (
[FlowId] INT NOT NULL primary key,
[SourceId] INT NOT NULL primary key,
[TargetId] INT NOT NULL primary key,
[Instance] NVARCHAR(255),
[File] NVARCHAR(255),
[AllTheOther] BIT NOT NULL
);

@2881099
Copy link
Collaborator

2881099 commented Mar 27, 2019

应该是个bug,修复好了通知你

@2881099
Copy link
Collaborator

2881099 commented Mar 27, 2019

use [Test];
CREATE TABLE [dbo].[ConnModel] (
[FlowId] INT NOT NULL primary key,
[SourceId] INT NOT NULL primary key,
[TargetId] INT NOT NULL primary key,
[Instance] NVARCHAR(255),
[File] NVARCHAR(255),
[AllTheOther] BIT NOT NULL
);

这条可以改正确了执行,再次迁移时,结构没变化不会重新创建。
我大概晚上可以处理这个问题,目前人在外地不方便。

2881099 pushed a commit that referenced this issue Mar 28, 2019
- 修复 SqlServer CodeFirst 迁移多主键的 bug #23
@2881099
Copy link
Collaborator

2881099 commented Mar 28, 2019

@zhusheping
前几天人在外地,sqlserver下这个bug已修复好。

同时增加了测试多主键迁移的单元测试:

image

@zhusheping
Copy link
Author

联合主键可以了。
var curd1 = fsql.GetRepository<Song, int>(); 这种联合主键的写法如何写?
var curd1 = fsql.GetRepository<Song, int,int,string>();?

@2881099
Copy link
Collaborator

2881099 commented Mar 31, 2019

谢谢反馈,目前是参考abp vnext的接口,它也没支持多主键的仓储。

多主键时候,用这个仓储会影响Find Get Delete TKey的方法,传入是实体的重载方法没影响。可以避开使用这几个方法。

我目前在整合DbContext,那里面会有与现在相同的仓储实现,并且包含了状态跟踪。目前基本完成开发,待测试完成可以考虑迁移过去使用。多主键的问题会在这里得以解决。

@2881099
Copy link
Collaborator

2881099 commented Mar 31, 2019

状态跟踪,即Find实体后,设置指定的属性值,修改时只会更新变化的字段。

然后目前也实现了乐观锁,更新是比较放心的。

目前还在测试中……

@2881099
Copy link
Collaborator

2881099 commented Apr 1, 2019

/// <summary>
/// 返回仓库类,适用联合主键的仓储类
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="that"></param>
/// <param name="filter">数据过滤 + 验证</param>
/// <returns></returns>
public static BaseRepository<TEntity> GetRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
	return new DefaultRepository<TEntity, int>(that, filter);
}

可以考虑在项目内实现这个扩展方法,适合联合主键的仓储类操作。
只是缺少 Find Get Delete TKey 的重载方法,其它功能不受影响。

@zhusheping
Copy link
Author

zhusheping commented Apr 19, 2019

using (var uow = fsql.CreateUnitOfWork())
{
var flowRepos = uow.GetRepository();
flowRepos.Insert(flow);
uow.Commit();
}

提示:
System.InvalidOperationException:“ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.”
stackoverflow:
SqlTransaction sqlTrans = scon.BeginTransaction();

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

flow 的值,和实体发给我测试一下

@zhusheping
Copy link
Author

zhusheping commented Apr 19, 2019 via email

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

收到,我正在路上,大约30分钟后测试

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

image

老哥,用你给的代码没有发生错误

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

我明白你的意思了,你是想id会联合主键下的自增是吧,谢谢反馈。正在支持,预计1小时后发布 nuget

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

MySql.Data.MySqlClient.MySqlException:“Incorrect table definition; there can be only one auto column and it must be defined as a key”

这种联合主键,带自增在 mysql 下不被支持

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

System.InvalidOperationException:“ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.”

试出你的错误了,sqlserver 下发生的,原因检查后再说明

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

最新结果:pgsql/sqlite/pgsql 同样的操作没报错

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

原因找到了,如果 SqlConnection 开启了事务,SqlCommand 要求同时设置 SqlConnection 和 SqlTransaction,才可以。

其他数据库的实现没有这个问题。。

2881099 pushed a commit that referenced this issue Apr 19, 2019
需要同时设置 SqlCommand.Connection + Transaction
@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

v0.5.2 nuget 包发布中,稍后再试。

@zhusheping
Copy link
Author

ok测试数据存储进去了。
但是有个问题。
image
Id为0了,导致后面和它相关的id都是0了。

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

Id = 0 的原因:
1、你没有给值,int 默认值被插入了进去;
2、你没有设置它为自增;

@2881099
Copy link
Collaborator

2881099 commented Apr 19, 2019

这个贴楼层太高了,下次可以考虑开新贴,哈哈

@2881099 2881099 closed this as completed May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants