У меня есть DataTable, который действует как контейнер для множества пользовательских Card
объектов. Некоторые из свойств этого класса являются другими пользовательскими классами, и один из них, кажется, мешает установить первичный ключ этой таблицы данных.
Когда я пишу:
GlobalVariables.CardList.PrimaryKey = new DataColumn[] { GlobalVariables.CardList.Columns["ID"] };
Я получаю ошибка:
System.ArgumentException: 'Невозможно удалить уникальное ограничение' Constraint1 '. Сначала удалите ограничение внешнего ключа 'CardList_Images'.
, где Images
- это пользовательский класс, содержащий шесть строк.
Я не вижу эту ошибку при использовании строки выше после создания новый DataTable, он появляется только когда я пытаюсь установить первичный ключ DataTable, который был прочитан из файла. Я проверил файл XML и не вижу ничего, что идентифицирует первичный ключ, поэтому я подумал, что должен назначать его при каждом запуске.
Я пытался не задавать ключ, но мне нужно использовать метод Rows.Find()
в другом месте программы и использование Rows.Select($"ID = '{ID}')
приводит к ошибке:
System.ArgumentException: «Столбец« Изображения »не принадлежит таблице CardList».
Я также попытался добавить строку:
GlobalVariables.CardList.Constraints.Clear()
, но это также приводит к первой ошибке.
Вот код, к которому я запускаю Создайте DataTable в первый раз, затем он добавляется в DataSet, который сохраняется в файл xml, затем загружается при следующем запуске программы:
CardListTable.TableName = "CardList";
CardListTable.Columns.Add("Object", typeof(string));
CardListTable.Columns.Add("ID", typeof(string));
CardListTable.Columns.Add("OracleID", typeof(string));
CardListTable.Columns.Add("MultiverseIDs", typeof(List<int>));
CardListTable.Columns.Add("MTGOID", typeof(int));
CardListTable.Columns.Add("TCGPlayerID", typeof(int));
CardListTable.Columns.Add("Name", typeof(string));
CardListTable.Columns.Add("Language", typeof(string));
CardListTable.Columns.Add("ReleaseDate", typeof(DateTime));
CardListTable.Columns.Add("URI", typeof(string));
CardListTable.Columns.Add("ScryfallURI", typeof(string));
CardListTable.Columns.Add("Layout", typeof(string));
CardListTable.Columns.Add("HighResImage", typeof(bool));
CardListTable.Columns.Add("Images", typeof(Images));
CardListTable.Columns.Add("ManaCost", typeof(string));
CardListTable.Columns.Add("CMC", typeof(double));
CardListTable.Columns.Add("Type", typeof(string));
CardListTable.Columns.Add("OracleText", typeof(string));
CardListTable.Columns.Add("Power", typeof(string));
CardListTable.Columns.Add("Toughness", typeof(string));
CardListTable.Columns.Add("Colours", typeof(List<string>));
CardListTable.Columns.Add("ColourIdentity", typeof(List<string>));
CardListTable.Columns.Add("Legalities", typeof(Legalities));
CardListTable.Columns.Add("Games", typeof(List<string>));
CardListTable.Columns.Add("Reserved", typeof(bool));
CardListTable.Columns.Add("Foil", typeof(bool));
CardListTable.Columns.Add("NonFoil", typeof(bool));
CardListTable.Columns.Add("Oversized", typeof(bool));
CardListTable.Columns.Add("Promo", typeof(bool));
CardListTable.Columns.Add("Reprint", typeof(bool));
CardListTable.Columns.Add("Variation", typeof(bool));
CardListTable.Columns.Add("Set", typeof(string));
CardListTable.Columns.Add("SetName", typeof(string));
CardListTable.Columns.Add("SetType", typeof(string));
CardListTable.Columns.Add("SetURI", typeof(string));
CardListTable.Columns.Add("SetSearchURI", typeof(string));
CardListTable.Columns.Add("ScryfallSetURI", typeof(string));
CardListTable.Columns.Add("RulingsURI", typeof(string));
CardListTable.Columns.Add("PrintsSearchURI", typeof(string));
CardListTable.Columns.Add("CollectorNumber", typeof(string));
CardListTable.Columns.Add("Digital", typeof(bool));
CardListTable.Columns.Add("Rarity", typeof(string));
CardListTable.Columns.Add("FlavourText", typeof(string));
CardListTable.Columns.Add("CardBackID", typeof(string));
CardListTable.Columns.Add("Artist", typeof(string));
CardListTable.Columns.Add("ArtistIDs", typeof(List<string>));
CardListTable.Columns.Add("IllustrationID", typeof(string));
CardListTable.Columns.Add("BorderColour", typeof(string));
CardListTable.Columns.Add("Frame", typeof(string));
CardListTable.Columns.Add("FrameEffects", typeof(List<string>));
CardListTable.Columns.Add("FullArt", typeof(bool));
CardListTable.Columns.Add("Textless", typeof(bool));
CardListTable.Columns.Add("Booster", typeof(bool));
CardListTable.Columns.Add("StorySpotlight", typeof(bool));
CardListTable.Columns.Add("Prices", typeof(Prices));
CardListTable.Columns.Add("RelatedURIs", typeof(RelatedURIs));
CardListTable.Columns.Add("PurchaseURIs", typeof(PurchaseURIs));
CardListTable.Columns.Add("CardFaces", typeof(List<CardFace>));
CardListTable.Columns.Add("RelatedCards", typeof(List<RelatedCard>));
CardListTable.Columns.Add("IsFoil", typeof(bool));
CardListTable.Columns.Add("CopiesInDecks", typeof(int));
CardListTable.Columns.Add("CopiesInStorage", typeof(int));
CardListTable.Columns.Add("StorageLocation", typeof(string));
CardListTable.PrimaryKey = new DataColumn[] { CardListTable.Columns["ID"] };
Установка первичного ключа здесь не вызывает ошибок. Класс Images, который вызывает проблемы:
public class Images
{
public string Small { get; set; }
public string Normal { get; set; }
public string Large { get; set; }
public string PNG { get; set; }
public string Art_Crop { get; set; }
public string Border_Crop { get; set; }
}
Ошибка отображалась для другого столбца ранее, столбца List, который я пытался удалить, и ошибка переключалась на столбец Images.
Этот код я использую для загрузки DataSet
из файла xml:
GlobalVariables.MyData.ReadXml(GlobalVariables.MasterFile);
GlobalVariables.CardList = GlobalVariables.MyData.Tables["CardList"];
GlobalVariables.CardList.PrimaryKey = new DataColumn[] { GlobalVariables.CardList.Columns["ID"] };
Последняя строка вызывает ошибку.