none
using logging table in EF RRS feed

  • Question

  • I want to create a table simply for logging purpose, such as user login, and very likely data changes logging in future.

    this table is for audit/tracing purpose, I have no intention to put in foreign keys at all, practically all data (non-key) will dump into the table.

    so, primary key is useless for this table, apart from datetime and few other columns for sorting purpose.

    the sql work or design are no issue for me.

    ***

    now, practically all my app data changes is at code level using entity framework.

    i know by design EF expecting a table to have primary key.

    if i create a view at sql level, create a dummy/virtual "primary key" which just invoking the sql NewId() uniqueidentifier, and let EF map to this view, i'm guessing it will work.

     what about when I want to create (just view and create functions at code/EF) new log, will EF throw error?

    is there any good approach for using logging table through EF?

    Friday, November 10, 2017 6:01 AM

Answers

  • so, primary key is useless for this table, apart from datetime and few other columns for sorting purpose

    Well EF will not work for a table that has no primary-key, no if ands or buts.

    • Marked as answer by Kelmen Monday, November 13, 2017 6:34 AM
    Friday, November 10, 2017 1:42 PM
  • Hi Kelmen,

    >>is there any good approach for using logging table through EF?

    As DA924x said, Entity Framework relies on every entity having a key value that it uses for tracking entities, I would suggest that you could add a primary key on logging table.

    You could also use raw SQL without primary key.

    For more information about raw SQL with entity framework, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kelmen Monday, November 13, 2017 6:34 AM
    Monday, November 13, 2017 5:28 AM
    Moderator

All replies

  • so, primary key is useless for this table, apart from datetime and few other columns for sorting purpose

    Well EF will not work for a table that has no primary-key, no if ands or buts.

    • Marked as answer by Kelmen Monday, November 13, 2017 6:34 AM
    Friday, November 10, 2017 1:42 PM
  • Hi Kelmen,

    >>is there any good approach for using logging table through EF?

    As DA924x said, Entity Framework relies on every entity having a key value that it uses for tracking entities, I would suggest that you could add a primary key on logging table.

    You could also use raw SQL without primary key.

    For more information about raw SQL with entity framework, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kelmen Monday, November 13, 2017 6:34 AM
    Monday, November 13, 2017 5:28 AM
    Moderator