0

I'd like to know if I can, with a SQLite database and SQLite-Net Exensions, add 2 foreign keys to the same table with, each time, one of the foreign keys empty.

My structure is the following:

[Table("Picture")]
public class Picture
{
    [PrimaryKey]
    public int Id { get; set; }

    public string Name { get; set; }

    [ForeignKey(typeof(Contact))] // => Allow Null ?
    public string TokenContact { get; set; }

    [ForeignKey(typeof(Profile))] // Allow Null ?
    public string TokenProfile { get; set; }
}

[Table("Contact")]
public class Contact
{
    [PrimaryKey]
    public string Token {get;set;}

    [OneToMany]   
    public ObservableCollection<Picture> CollectionPicture {get; set;}
 }

[Table("Profile")]
public class Profile:Contact
{

// Some other properties...

 }

Thanks for your advices !

GwenGuts
  • 497
  • 1
  • 4
  • 12
  • 1
    I don't get what you're trying. You have a one-to-many relationship from 'Contact' to 'Picture'. The foreign key is 'TokenContact'. Probably that works fine. What is the 'TokenProfile' property? – redent84 Feb 09 '15 at 20:25
  • @redent84 Thanks for your answer. It's because Contact and Profile can have both one or many Pictures. So, I want to reference the token of Contact or Profile in the Picture entity. And it seems impossible to reference one token for both of the Contact and Profile entities. So, it's the reason I want to know if you can put 2 foreign keys in Picture allowing null foreach of them. -Table Contact: Token, Name -Table Profile: Token, Name, [other stuff] -Table Picture: Id, Name, FK_TokenContact (NULL), FK_TokenProfile (NULL) Thanks ! – GwenGuts Feb 10 '15 at 08:20
  • I see. Yes, all foreign keys are nullable by default. Is that schema giving you any issue? – redent84 Feb 10 '15 at 08:39
  • @redent84 So, it's not a problem if those FK are nullable by default :). On the contrary, it's what I wanted, the possibility to have this kind of data scheme: - TABLE Picture: - Entry 1: Id:1, Name:"Test1.jpg", TokenContact:"212354545645646", TokenProfile:NULL - Entry 2: Id:2, Name:"Test2.jpg", TokenContact:NULL, TokenProfile:"212354545645647" So, that example is possible with sqlite-net extensions (as I showed it in my original question) ? – GwenGuts Feb 10 '15 at 08:53
  • 1
    Yes is it possible. It should work out of the box. Have you tested it? – redent84 Feb 10 '15 at 09:14
  • @redent84 Not yet, I will try that ASAP – GwenGuts Feb 10 '15 at 13:23
  • @GwenGuts did you tried? I have a similar mapping except that i have both the object and an id mapped for the foreign key and if i dont suply a value for the FK i get constraint error. if i insert directly on database it allows nulls...so looks like it's comming from the library – J.J Apr 17 '20 at 20:24

0 Answers0