1

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; }
  }

1 Answer 1

0

I think I solved it myself. I just did this: How to re-create database for Entity Framework?

If anyone still could tell me why this happend I'd be happy

Thank you!

Sign up to request clarification or add additional context in comments.

2 Comments

You probably changed your model in a way migrations could not handle by default - changing the identity status, FK renames, are examples. By deleting everything and starting over you avoid the renames and everything is created from scratch. stackoverflow.com/questions/17894906/…
What if you can't start from scratch?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.