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

OneToOne 一对一,怎么添加数据? #45

Closed
2881099 opened this issue Apr 28, 2019 · 1 comment
Closed

OneToOne 一对一,怎么添加数据? #45

2881099 opened this issue Apr 28, 2019 · 1 comment

Comments

@2881099
Copy link
Collaborator

2881099 commented Apr 28, 2019

需要安装FreeSql.DbContext

dotnet add package FreeSql
dotnet add package FreeSql.DbContext
dotnet add package FreeSql.Provider.MySqlConnector

无事务的做法

var repoA = orm.GetRepository<A>();
var repoB = orm.GetRepository<B>();

var a = new A();
repoA.Insert(a);
var b = new B { id = a.Id };
repoB.Insert(b);

reposA.Insert(a);,如果 A 有自增,会自动填充 a.id 插入后的值。

后面就可以直接使用 a.id 来添加 b了。

@2881099
Copy link
Collaborator Author

2881099 commented Apr 28, 2019

事务的做法

using (var uow = orm.CreateUnitOfWork()) {
    var repoA = orm.GetRepository<A>();
    var repoB = orm.GetRepository<B>();

    var a = new A();
    repoA.Insert(a);
    var b = new B { id = a.Id };
    repoB.Insert(b);

    uow.Commit();
}

This issue was closed.
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

1 participant