question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Excluding the default Id primary key from an Entity....

See original GitHub issue

Hi,

I have the following structure defined for my entities:

/// <summary>
///     Base class for all entities defined in the system. This allows for the following
///     COMMON features:
///     - Primary key "Id" of type long
///     - Auditing which adds Date & User information for creation and update
/// </summary>
public class CcaEntityCommon : AuditedEntity<long>
{
}

/// <summary>
///     Extends the common functionality by adding the ability to mark a record as deleted rather than
///     deleting it from the database. Unless there is a reason not to mark an entity as a soft delete
///     then all entities should descend from this class.
/// </summary>
public class CcaEntityBase : CcaEntityCommon, ISoftDelete
{
    #region Implementation of ISoftDelete

    /// <summary>
    ///     Used to mark an Entity as 'Deleted'.
    /// </summary>
    public bool IsDeleted { get; set; }

    #endregion
}

And ultimately a table defined as:

[Table(“ContractorsEmployees”, Schema = “dbo”)] public class ContractorEmployeeTableModel : CcaEntityBase { #region Public Members

    public ContractorTableModel Contractor { get; set; }

    [Key, Column(Order = 1)]
    [ForeignKey("Contractor")]
    public long ContractorId { get; set; }

    public EmployeeTableModel Employee { get; set; }

    [Key, Column(Order = 2)]
    [ForeignKey("Employee")]
    public long EmployeeId { get; set; }

    #endregion
}

However in this particular case the primary key is ContractorId AND EmployeeId and I don’t need the Id key. So how do I exclude it in ContractorEmployeeTableModel? Can I do it with a data annotation? Or fluent API? Or do I have to have a “different” inheritance hierachy for tables that have compound keys?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hikalkancommented, Mar 3, 2016

Can you map Id to your primary key? Even if their name different, you can override Id and add a [Column(“YourPkName”)] attribute to change it.

0reactions
sohaibjaved44commented, Mar 7, 2016

Yes did the same and its good solution. thanks. I’m wondering why I couldnt think that way 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Can you remove Identity from a primary key with Entity ...
Look at the ALTER TABLE - ALTER COLUMN statement: there is no syntax to change (add or remove) an identity specification. Refer flowing...
Read more >
Add or change a table's primary key in Access
Access can automatically create a primary key field for you when you create a table, or you can specify the fields that you...
Read more >
Documentation: 15: CREATE TABLE
Indexes, PRIMARY KEY , UNIQUE , and EXCLUDE constraints on the original table will be created on the new table. Names for the...
Read more >
DAD220 Ch 3 Flashcards
No. (ID, Relationship) cannot be the primary key because (6381, Daughter) is repeated. Composite primary keys must be unique.
Read more >
Define data using Room entities
By default, Room uses the class name as the database table name. ... Each Room entity must define a primary key that uniquely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found