Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Cannot set value of Manual Identity Type #100

Closed
RuiAlias opened this issue Sep 30, 2019 · 4 comments
Closed

Cannot set value of Manual Identity Type #100

RuiAlias opened this issue Sep 30, 2019 · 4 comments
Labels
duplicate Duplicate issue question Requesting information

Comments

@RuiAlias
Copy link
Contributor

RuiAlias commented Sep 30, 2019

Hi, I'm trying to create an entity with not-auto-generated primary key. To achieve that, I created an entity and modified the identity property to have an "Identity Type" of "Manual".

I expected to see this property being generated in the entity's constructor, since it's required and not-auto-generated. However, that's not what's happening. The generated class does not expose a way to set the value of the identity property: the property does not appear in the constructor and its setter is private.

As such, I cannot create entities of this type in my code.


Example

image

image

         modelBuilder.Entity<global::BottomUp.Model.Currency>()
                     .ToTable("Currency")
                     .HasKey(t => t.Code);

         modelBuilder.Entity<global::BottomUp.Model.Currency>()
                     .Property(t => t.Code)
                     .HasMaxLength(3)
                     .IsRequired()
                     .ValueGeneratedNever();
      /// <summary>
      /// Public constructor with required data
      /// </summary>
      /// <param name="name">Name of language in English</param>
      /// <param name="symbol">Currency symbol</param>
      public Currency(string name, string symbol)
      {
         if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
         this.Name = name;
         if (string.IsNullOrEmpty(symbol)) throw new ArgumentNullException(nameof(symbol));
         this.Symbol = symbol;
         Init();
      }

      /// <summary>
      /// Static create function (for use in LINQ queries, etc.)
      /// </summary>
      /// <param name="name">Name of language in English</param>
      /// <param name="symbol">Currency symbol</param>
      public static Currency Create(string name, string symbol)
      {
         return new Currency(name, symbol);
      }

      /*************************************************************************
       * Persistent properties
       *************************************************************************/

      /// <summary>
      /// Identity, Required, Indexed, Min length = 3, Max length = 3
      /// Unique record identifier Examples: EUR; USD
      /// </summary>
      /// <remarks>
      /// Unique record identifier
      /// </remarks>
      [Key]
      [Required]
      [MaxLength(3)]
      [StringLength(3)]
      public string Code { get; private set; }
@msawczyn msawczyn added the question Requesting information label Sep 30, 2019
@msawczyn
Copy link
Owner

Each constructor calls the partial method Init which allows you to add constructor logic in a central location and have it execute in every constructor (note, though, that if you add your own constructors, you're on the hook for calling Init from that constructor as well :-) ). In the current version, this is typically where you would set the entity's identifier.

In the very-soon-to-be-released v1.3.0.5, though, manually-set identifiers have indeed been added to the public constructors. See #93 .

Hope that helps.

@msawczyn msawczyn added the duplicate Duplicate issue label Sep 30, 2019
@RuiAlias
Copy link
Contributor Author

Thanks! And sorry for the duplicate issue.

It certainly helps. The Init solution doesn't work for what I want to do though.

I will try to compile the latest version.

@msawczyn
Copy link
Owner

msawczyn commented Sep 30, 2019 via email

@RuiAlias
Copy link
Contributor Author

RuiAlias commented Sep 30, 2019

Oh, awesome!

I gave compilation a quick shot but there were some problems. However I haven't yet read the Contributing article so I'm probably missing something. I'll leave that for another time since I have no urgency now.

Thanks

Edit: Feel free to close this issue.

@msawczyn msawczyn closed this as completed Oct 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate Duplicate issue question Requesting information
Projects
None yet
Development

No branches or pull requests

2 participants