Note that there are some explanatory texts on larger screens.

plurals
  1. POBest approach to insert on many to many tables using Identity columns on ServiceStack ORMLite
    primarykey
    data
    text
    <p>Yesterday I found this great ORM and would like to perform some testings on more complex stuff than the samples provided on github.</p> <p>This is the model I'm using</p> <pre><code>class BaseClass { [AutoIncrement] public int Id { get; set; } [StringLength(200)] public string Name { get; set; } } [Alias("artist")] class Artist : BaseClass { } [Alias("genre")] class Genre : BaseClass { } [Alias("artistgenre")] class ArtistGenre { [PrimaryKey] public int Id { get; set; } [Alias("idgenre")] [References(typeof(Genre))] public int IdGenre { get; set; } [Alias("idartist")] [References(typeof(Artist))] public int IdArtist { get; set; } } </code></pre> <p>And this is what I'm trying to achieve (although it doesn't work since the Identity values are unknown when inserting into artistgenre table).</p> <pre><code> if (dbConn.TableExists("artistgenre")) dbConn.DropTable&lt;ArtistGenre&gt;(); if (dbConn.TableExists("artist")) dbConn.DropTable&lt;Artist&gt;(); if (dbConn.TableExists("genre")) dbConn.DropTable&lt;Genre&gt;(); dbConn.CreateTables(false, typeof(Artist), typeof(Genre), typeof(ArtistGenre)); var genres = new List&lt;Genre&gt;(); genres.Add(new Genre() { Name = "Rock" }); genres.Add(new Genre() { Name = "Pop" }); genres.Add(new Genre() { Name = "Jazz" }); genres.Add(new Genre() { Name = "Classic" }); genres.Add(new Genre() { Name = "Bossanova" }); var artists = new List&lt;Artist&gt;(); artists.Add(new Artist() { Name = "Rush" }); artists.Add(new Artist() { Name = "Queen" }); artists.Add(new Artist() { Name = "Pat Metheny" }); var ag = new List&lt;ArtistGenre&gt;(); var a = artists.FirstOrDefault(c =&gt; c.Name == "Rush"); var g = genres.FirstOrDefault(c =&gt; c.Name == "Rock"); ag.Add(new ArtistGenre() { IdArtist = a.Id, IdGenre = g.Id }); dbConn.SaveAll&lt;Artist&gt;(artists); dbConn.SaveAll&lt;Genre&gt;(genres); dbConn.SaveAll&lt;ArtistGenre&gt;(ag); </code></pre> <p>Is there a simple solution other than adding each row and obtaining its Identity values?</p> <p>Thanks. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload