C# 一个或多个文件与数据库的主文件不匹配(错误 5173)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/120625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One or more files do not match the primary file of the database (error 5173)
提问by Graviton
I got this error when I checked out my database from source control. It might sounds weird to check in the sql server database, but this was what I have done because this is just a personal project.
当我从源代码管理中检出我的数据库时出现此错误。检查 sql server 数据库可能听起来很奇怪,但这就是我所做的,因为这只是一个个人项目。
Anyone knows how to fix this?
有谁知道如何解决这个问题?
采纳答案by Graviton
Here's my finding.
这是我的发现。
As mentioned by other posters, you really don't want to check database files into and out of the source control.
正如其他海报所提到的,您真的不想将数据库文件检入和检出源代码管理。
But if you absolutely need to, and you have done check in the database files and you are encountering the same error that I encountered, here is a workaround:
但是,如果您绝对需要,并且您已经检查了数据库文件并且遇到了与我遇到的相同的错误,这里有一个解决方法:
First, detach the database, then, delete the ldf file, reattach the database again.
首先,分离数据库,然后,删除ldf文件,重新连接数据库。
This is how I solved my problem.
这就是我解决我的问题的方法。
回答by Cade Roux
You really don't want to be checking database files into and out of source control - in SQL Server you have to detach the files for this to even work and you run all kinds of risks.
您真的不想检查数据库文件进出源代码控制 - 在 SQL Server 中,您必须分离文件才能使其正常工作,并且您会冒各种风险。
If you absolutely have to do this, you should version backups.
如果您绝对必须这样做,则应该进行版本备份。
I recommend versioning a script which creates the entire database (tables, sprocs, views, etc.)
我建议对创建整个数据库(表、sproc、视图等)的脚本进行版本控制。
You can try creating a database attaching from that data file and using Create Databasethe ATTACH_REBUILD_LOG
option, but I'm not confident it's going to work since they probably weren't detached properly.
您可以尝试从该数据文件创建一个附加的数据库并使用“创建数据库”ATTACH_REBUILD_LOG
选项,但我不确定它会起作用,因为它们可能没有正确分离。
回答by Rob
Did you take a copy of the log file (.ldf) as well as the ".mdf" file? You need the matching set of both to re-attach the database
您是否复制了日志文件 (.ldf) 和“.mdf”文件?您需要两者的匹配集才能重新附加数据库
回答by Georgi
This sounds like the data files do not match the structure files of your database.
这听起来像数据文件与数据库的结构文件不匹配。
Shortly spoken, the files where your data (i.e. table rows) resides in are (mostly) not the files the structure of your data (i.e. the description of the tables) is stored. At least in "modern" RDBMS systems. So you checked out your data and the database recognized some changes in the structure which happened till then (you altered a table or something like that).
简而言之,数据(即表行)所在的文件(大部分)不是存储数据结构(即表的描述)的文件。至少在“现代”RDBMS 系统中。因此,您检查了您的数据,并且数据库识别出在此之前发生的结构中的一些更改(您更改了表或类似的东西)。
The way "to fix this" would be to check in all files your database relies on, but I think that is not really what you wanted to achieve. Better (as mentioned above) to do backups and then drop / restore the database from them.
“解决这个问题”的方法是检查数据库所依赖的所有文件,但我认为这并不是您真正想要实现的。最好(如上所述)进行备份,然后从中删除/恢复数据库。