简体   繁体   English

SSDT TSqlModel.DeleteObjects 方法的行为不符合预期

[英]SSDT TSqlModel.DeleteObjects method doesn't behave as expected

I've been trying to use the TSqlModel method DeleteObjects to programmatically remove certain users from a Database project.我一直在尝试使用 TSqlModel 方法 DeleteObjects 以编程方式从数据库项目中删除某些用户。 The problem is that when I call the method, the user remains in the model.问题是当我调用该方法时,用户仍留在模型中。 I wonder if I am calling the method correctly.我想知道我是否正确调用了该方法。 Here's something close to what I am doing:这是接近我正在做的事情:

modelFromDacpac.DeleteObjects(@"DOMAIN\user");

When I run the following code to see if it's really gone, the user is still there!当我运行下面的代码看它是否真的消失了时,用户还在!

var tst_delete= modelFromDacpac.GetObjects(User.TypeClass, new ObjectIdentifier(@"DOMAIN\user"), DacQueryScopes.Default).FirstOrDefault();

tst_delete is non-null and has a name that matches "DOMAIN\\user\u0026quot; . tst_delete是非空的,并且具有匹配"DOMAIN\\user\u0026quot;

Any idea what I'm doing wrong?知道我做错了什么吗?

Prior to the DeleteObject method call, I insert the following line - where the sqlobj object is a TSqlObject referring to the user I am trying to deleteDeleteObject方法调用之前,我插入以下行 - 其中 sqlobj 对象是一个TSqlObject指的是我试图删除的用户

//For some reason, the logins aren't scripted objects within the DACPAC, and so cannot be deleted using the DeleteObjects method - or maybe they simply cannot be found.
modelFromDacpac.ConvertToScriptedObject(sqlobj, "DOMAIN_user.sql");

Then I call the DeleteObject method as follows:然后我调用DeleteObject方法如下:

modelFromDacpac.DeleteObjects("DOMAIN_user.sql");

I'm not sure why this works, but it does.我不确定为什么会这样,但确实如此。 My guess is that the DeleteObject method is pretty picky about how and where it expects to find objects.我的猜测是, DeleteObject方法对于它期望如何以及在何处查找对象非常挑剔。 Or, maybe some objects, like users, are stored in some non-standard fashion which prevents DeleteObjects from finding them.或者,可能某些对象(例如用户)以某种非标准方式存储,这会阻止DeleteObjects找到它们。 Whatever the reason, but explicitly converting the user to a scripted object with a given name, and passing that given name to the DeleteObjects method, it works.不管是什么原因,但显式地将用户转换为具有给定名称的脚本对象,并将该给定名称传递给DeleteObjects方法,它可以工作。

I am a little concerned that I do not know why it works.我有点担心我不知道它为什么起作用。 The other concern is that it doesn't show up in the official documentation of the TSqlModel object: https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dac.model.tsqlmodel_methods(v=sql.120).aspx另一个问题是它没有出现在TSqlModel对象的官方文档中: https : TSqlModel = TSqlModel . 120).aspx

But it does work.但它确实有效。 At least, so far.至少,到目前为止。

DeleteObject caught me out the same way :) - it only deletes scripts added using AddOrUpdate when you also pass in a script name and Delete uses the same script name. DeleteObject 以同样的方式吸引了我:) - 当您还传入脚本名称并且 Delete 使用相同的脚本名称时,它只会删除使用 AddOrUpdate 添加的脚本。

What you need to do is create a new model and add in everything except the things you want to delete.您需要做的是创建一个新模型并添加除要删除的内容之外的所有内容。

Why do you want to delete a login?为什么要删除登录? If you don't want it to be deployed you can use a deployment contributor like my one here to exclude the login at deployment time:如果你不想部署它,你可以使用像我这里的部署贡献者来排除部署时的登录:

https://the.agilesql.club/Blogs/Ed-Elliott/HOWTO-Filter-Dacpac-Deployments https://the.agilesql.club/Blogs/Ed-Elliott/HOWTO-Filter-Dacpac-Deployments

Ed埃德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM