I’ve got a problem with Entity Framework and doing a Migration (database-update) and Foreign Key Annotations.
The error message that I get is after: update-database –verbose
Applying automatic migration: 201604060846578_AutomaticMigration.
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_OutreachSet_TargetContact' AND object_id = object_id(N'[dbo].[DiagnosticsSets]', N'U'))
DROP INDEX [IX_OutreachSet_TargetContact] ON [dbo].[DiagnosticsSets]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_OutreachSet_TargetContact' AND object_id = object_id(N'[dbo].[MotivationSets]', N'U'))
DROP INDEX [IX_OutreachSet_TargetContact] ON [dbo].[MotivationSets]
DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.MotivationSets')
AND col_name(parent_object_id, parent_column_id) = 'TargetContact';
IF @var0 IS NOT NULL
EXECUTE('ALTER TABLE [dbo].[MotivationSets] DROP CONSTRAINT [' + @var0 + ']')
ALTER TABLE [dbo].[MotivationSets] DROP COLUMN [TargetContact]
[...]
The object 'PK_dbo.MotivationSets' is dependent on column 'TargetContact'.
ALTER TABLE DROP COLUMN TargetContact failed because one or more objects access this column.
I’d just like to have a 0:0…1 Relationship. That's it.
I’d tried to do it just like in this example EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship
public class OutreachSet
{
[Key]
[Display(Name = "Target Contact")]
public string TargetContact { get; set; }
[Display(Name = "Next Outreach Step")]
public string NextOutreachStep { get; set; }
public virtual MotivationSet MotivationSet { get; set; }
}
public class MotivationSet
{
[Key, ForeignKey("OutreachSet")]
public string TargetContact { get; set; }
public string PowerMastery { get; set; }
[Required]
public virtual OutreachSet OutreachSet { get; set; }
}