MightyOrm / Mighty

A new, small, dynamic micro-ORM. Highly compatible with Massive, but with many essential new features.
BSD 3-Clause "New" or "Revised" License
101 stars 20 forks source link

SQL server Guid PK #41

Closed GusBeare closed 2 years ago

GusBeare commented 2 years ago

I'm playing with 4.0.0-aplha-3 in .Net 5. I have a table with a Guid PK. When I try to insert like this:

  var db = new MightyOrm<Code.Models.Franchise>(cn, "Franchise", "Guid");
  var f = db.New();

  f.Guid = Guid.NewGuid();
  f.Name = Franchise.Name;
  f.Created = Franchise.Created;
  f.CreatedBy = f.CreatedBy;

  db.Insert(f);

On the line db.New() I get "InvalidCastException: Invalid cast from System.String to System.Guid".

Does Mighty handle Guid PK's?

mikebeaton commented 2 years ago

As currently written, Mighty assumes integer PKs. You should be able to treat Franchise as a table with no PK and have the above work. You'll need to fetch by using method overloads which accept explicit column name and value (e.g. the first example here https://mightyorm.github.io/Mighty/docs/getting-started.html#reading-multiple-rows ) - rather than just PK value as you can when you have specified the integer PK column.

GusBeare commented 2 years ago

Thanks Mike. Yes this works fine.

        MightyOrm franchise = new MightyOrm(cn, "Franchise");
        IEnumerable<dynamic> allFranchises = franchise.All();
        var row = allFranchises.FirstOrDefault();